2020年计算机算法设计与分析课程设计常规题目的C及C代码集.pdf
计算机算法设计与分析课程设计常规题目的C及C代码集文档仅供参考,不当之处,请联系改正。合并排序1:#includeusing namespace std;const int N=100;class list(public:int arrayN;void input(int a);void merge(int arrayc,int arrayd,intl,int m,int r);void mergepass(int arrayx,intarrayy,int s);void mergesort(int arrayl);void diaplay(int a););void list:input(int a)(coutMPlease input shorted array:nendl;for(int i=0;ia;i+)cinarrayi;)文档仅供参考,不当之处,请联系改正。void list:merge(int arrayc,int arrayd,int l,intm,int r)(int i=l;int j=m+l;int k=l;while(i=m)&(j=r)if(arraycim)for(int q=j;q=r;q+)arraydk+=arraycq;elsefor(int q=i;q=m;q+)arraydk+=arraycq;)void list:mergepass(int arrayx,int arrayy,ints)int i=0;while(i=N-2*s)文档仅供参考,不当之处,请联系改正。merge(arrayx,arrayy,i,i+s-l,i+2*s-l);i=i+2*s;)if(i+s)N)merge(arrayx,arrayy,i,i+s-l,N-l);elsefor(int j=i;jN;j+)arrayyj=arrayxj;)void list:mergesort(int array 1)(int array2N;int s=l;while(sN)(mergepass(arrayl,array2,s);s+=s;mergepass(array 2,array 1,s);)s+=s;)文档仅供参考,不当之处,请联系改正。void list:diaplay(int a)(for(int i=N-a;iN;i+)coutarrayi;)void main()(list f;int a;coutvv”请输入要合并排序的数组大小:(数组最大上限100)”vveii祖;cina;f.input(a);f.mergesort(f.array);f.diaplay(a);)合并排序:2#include usingnamespace std;void MERGES(int*A,int p,int q,int r)下标文档仅供参考,不当之处,请联系改正。P=qrint nl=q-p+l;/nl:p,q之间的数的个数int n2=r-q;/n2:q 以后到r 的数的个数int*L=newint nl+1,动态申请两个子数组*R=newint n2+l;int i,j,k;for(i=0;inl;i+)(Li=Ap+i-l;)for(j=0;jn2;j+)(Rj=Aq+j;)Lnl=10000;设置哨兵Rn2=10000;i=0;j=0;for(k=p-l;kr;k+)文档仅供参考,不当之处,请联系改正。if(Li=Rj)(Ak=Li;i=i+l;)else(Ak=RU;j=j+l;)void MERGESORT(int*A,int p,int r)(if(Pr)(int q=(p+r)/2;MERGESORT(A,p,q);MERGESORT(A,q+l,r);MERGES(A,p,q,r);)文档仅供参考,不当之处,请联系改正。void main()int x,z,p,r;cout”请输入数组长度 v Vendl;cinx;int*A=newintx;cout 请输入数组的元素 vvendl;for(int y=0;yx;y+)(cinz;Ay=z;)coutvv”请输入上下限p,rnendl;cinpr;MERGESORT(A,p,r);cout 合并排序后为:vVendl;for(int m=p;m=r;m+)(coutAm;)delete A;)文档仅供参考,不当之处,请联系改正。合并排序3:#include#include 这个函数将 b0至bright eft+l拷贝到 至 arighttemplate void Copy(T a,T b,int left,int right)(int size=right-left+l;for(int i=0;isize;i+)(aleft+=bi;)这 个 函 数 合 并 有 序 数 组到 b,得到新的有序数组btemplate void Merge(T a,T b,int left,int i,int right)(int alcout=leftj/指向第一个数组开头alend=i指向第一个数组结尾a2cout=i+l,指向第二个数组开头a2end=right,指向第二个数组结尾文档仅供参考,不当之处,请联系改正。bcout=0;指向b 中的元素for(int j=0;j alend)(bbcout+=aa2cout+;continue;如果第一个数组结束,拷贝第二个数组的元素到bif(a2couta2end)(bbcout+=aalcout+;continue;如果第二个数组结束,拷贝第一个数组的元素到bif(aalcoutaa2cout)(bbcout+=aalcout+;continue;如果两个数组都没结束,比较元素大小,把较小的放入b文档仅供参考,不当之处,请联系改正。elsebbcout+=aa2cout+;continue;)对 数 组 right进行合并排序template void MergeSort(T a,int left,int right)(T*b=new intright-left+l;if(leftright)(int i=(left+right)/2;取中点MergeSort(a,left,i);左半边进行合并排序MergeSort(a,i+l,right);右半边进行合并排序Merge(a,b,Ieft,i,right);左右合并到 b 中Copy(a,b,left,right);/A b 拷贝回来)/fromint main()文档仅供参考,不当之处,请联系改正。int n;coutnhow many numbers to sort:;cinn;int*a=new intn;coutninput n n n numbers:n;for(int i=0;in;i+)(cinai;)MergeSort(a,0,n-1);for(int j=O;jn;j+)(coutsetw(5)aj;)return 1;)合并排序4:#include using namespace std;void Merge(int a,int b,int l,int m,int r)文档仅供参考,不当之处,请联系改正。int i=I,j=m+l,k=l;while(i=m)&(j=r)if(aim)for(int q=j;q=r;q+)bk+=aq;elsefor(int q=i;q=m;q+)bk+=aq;)void Copy(int a,int b,int s,int n)(for(int i=s;i=n;i+)ai=bi;)void MergeSort(int a,int left,int right)(int i;if(leftright)文档仅供参考,不当之处,请联系改正。i=(left+right)/2;intb100;MergeSort(a,left,i);MergeSort(a,i+l,right);Merge(a,b,left,i,right);Copy(a,b,left,right);)int main()(int a100;int n,i;coutvv”输入元素个数n:u;cinn;coutvv”输入一维数组 an n n:n;for(i=0;in;i+)cinai;MergeSort(a,0,n-l);coutvv”输出排序为:“;for(i=0;in;i+)coutai,coutendl;文档仅供参考,不当之处,请联系改正。return 0;)矩阵相乘1:#include#include using namespace std;int main()(int i,j,k;coutvv”输入二维数组a 的行数和二维数组c的行数x:n;int x;cinx;coutvv”输入二维数组a 的列数和二维数组b的行数y:n;inty;ciny;coutvv”输入二维数组b 的列数和二维数组c的行数Z:”;int z;cinz;文档仅供参考,不当之处,请联系改正。int*a,*b,*c;a=new int*x;for(i=0;ix;i+)(ai=new inty;)coutvv”输入二维数组 a:Hendl;for(i=0;ix;i+)(for(j=0;jy;j+)(cinaij;)b=new int*y;for(i=0;iy;i+)(bi=new int z;)coutvv”输入二维数组 b:nendl;for(i=0;iy;i+)文档仅供参考,不当之处,请联系改正。for(j=0;jz;j+)(cinbij;)c=new int*x;for(i=0;ix;i+)(ci=new int z;)coutvv”输入二维数组 c:nendl;for(i=0;ix;i+)(for(j=0;jz;j+)(ciU=0;)for(i=0;ix;i+)for(j=0;jz;j+)文档仅供参考,不当之处,请联系改正。for(k=0;ky;k+)(cij+=aik*bkj;)for(i=0;ix;i+)(for(j=0;jz;j+)(coutvvcijvv,;)coutendl;)for(i=0;ix;i+)(delete ai;)delete a;for(i=0;ix;i+)delete ci;文档仅供参考,不当之处,请联系改正。)delete c;for(i=0;iy;i+)(delete bi;)delete b;return 0;)矩阵相乘2:#includeusing namespace std;#define M 2#define N 3#define P 4int main()(int aMN=l,2,3,4,5,6);intbNP=7,8,9,2,3,4,5,6,7,8,9;int cMP;int i,j,k;文档仅供参考,不当之处,请联系改正。for(i=0;iM;i+)for(j=0;jP;j+)cij=O;for(i=0;iM;i+)for(j=0;jP;j+)for(k=0;kN;k+)cij+=aik*bkj;cout n矩阵相乘结果是:vVendl;for(i=0;iM;i+)for(j=0;jP;j+)coutcijn n;coutendl;/system(n pause);return 0;)矩阵相乘3:#include#include using namespace std;int main()const int m=3;文档仅供参考,不当之处,请联系改正。const int n=3;intamn,i,j;初始化数组 a,bfor(i=0;ivm;i+)对数组a 赋值并显示(for(j=O;jn;j+)(coutnan i nnnn j n=n;cinaij;)for(i=0;im;i+)(for(j=O;jn;j+)coutsetw(4)ai j;coutendl;)const int k=3;const int h=2;int bkh,x,y;for(x=0;xk;x+)for(y=0;yh;y+)文档仅供参考,不当之处,请联系改正。cou tnbn x nnnn y n=n;cinbxy;)for(x=O;xk;x+)(for(y=O;yh;y+)coutsetw(4)b x y;coutendl;)int cmh;/乘赋值给数组cfor(int r=O;rm;r+)(for(int t=O;th;t+)数组 a 与 b 相对数 组b赋值并显示(crt=O;for(int s=O;sk;s+)(crt+=ars*bst;)文档仅供参考,不当之处,请联系改正。)cou tncn m nnnn h nHendI;for(int z=O;zm;z+)(for(int w=O;wh;w+)coutsetw(4)cz w;coutendl;)return 0;显示数组c)矩阵相乘4:#includeusing namespace std;void chain(int*p,int n,int m7,mt s7)/p 维数数组,m最优乘次数组,s记录划分方案(intj;for(int i=l;i=n;i+)mii=0;for(int r=2;r=n;r+)文档仅供参考,不当之处,请联系改正。for(i=l;i=n-r+l;i+)(j=i+r-l;mij=mi+lj+pi-l*pi*pj;sij=i;for(int k=i+l;kj;k+)(intt=mik+mk+lj+pi-l*pk*pj;if(t=i;j-)文档仅供参考,不当之处,请联系改正。coutm ijM;)coutendl;)void Traceback(int i,int j,int s口 7)输出相乘方案(if(i=j)return;Traceback(i,sij,s);Traceback(sij+l,j,s);cout HMultiply An i n,nsij;coutand B(sij+l)n,H j en d l;return;)int main()(intp7,m77,s77,n;while(n!=EOF)for(int i=0;i=n;i+)文档仅供参考,不当之处,请联系改正。cinpiendl;)chain(p,n,m,s);Traceback(l,6,s);)return 0;)Haffman 编码 1:#include#includeusing namespace std;typedef struct(int weight;int flag;int parent;int lehild;int rchild;)hnodetype;文档仅供参考,不当之处,请联系改正。typedef struct(int bit10;int start;char leaf;)hcodetype;void huf(char cha,int m,mt n)(int i,j,ml,m2,xl,x2,c,p;hnodetype*huffnode=new hnodetype2*n-l;hcodetype*huffcode=new hcodetypen,cd;for(i=0;i2*n-l;i+)(huffnodei.weight=0;huffnodei.parent=0;huffnodei.flag=0;huffnodei.lchild=-l;huffnodei.rchild=-l;)for(i=0;in;i+)文档仅供参考,不当之处,请联系改正。huffnodei.weight=mi;huffcodei.leaf=chai;)for(i=0;in-l;i+)(ml=m2=10000000;xl=x2=0;for(j=0;jn+i;j+)(if(huffnodej.weight=ml&huffnodej.flag=0)(m2=ml;x2=xl;ml=huffnodej.weight;xl=j;)elseif(huffnodej.weight=m2&huffnodej.flag=0)文档仅供参考,不当之处,请联系改正。m2=huffnodej.weight;x2=j;)huffnodexl.parent=n+i;huffnodex2.parent=n+i;huffnodexl.flag=l;huffnodex2.flag=l;huffnoden+i.weight=huffnodexl.weight+huffnodex2.weight;huffnoden+i.lchild=xl;huffnoden+i.rchild=x2;)for(i=0;in;i+)(cd.start=n-l;c=i;p=huffnodec.parent;while(p!=O)if(huffnodep.lchild=c)文档仅供参考,不当之处,请联系改正。cd.bitcd.start=O;elsecd.bitcd.start=l;cd.start;c=p;p=huffnodec.parent;)couthuffcodei.leaffor(j=cd.start+l;jn;j+)(huffcodei.bitj=cd.bitj;coutcd.bitj;)coutendl;huffcodei.start=cd.start;)delete huffcode;delete huffnode;)void main()string str;文档仅供参考,不当之处,请联系改正。int i=0,j,n,m100,h,k=0;char cha100;coutvv”请输入一个字符串nendl;cinstr;n=str.length();coutn字 符 串 总 共 有 字 符n n n个nendl;for(i=0;in;i+)(j=0;h=0;while(stri!=strj)j+;(chak=stri;cout 字符 chak 出现;)else continue;for(j=i;jn;j+)if(stri=strj)h+;文档仅供参考,不当之处,请联系改正。)couth 次v vendl;mk=h;k+;)coutvv”每个字符的哈夫曼编码是:nendl;huf(cha,m,k);cin.get();cin.get();)Haffman 编码 2:#include using namespace std;*构 造 哈yvA i s:尤/不 不 不 不 不 不 不 不 不 不 不 不 不 不 不 不 不 不 不 不 不 不 不 不 不 不 不 不 不 不 不 不/不哈夫曼树顺序表的定义*/typedef structint weight;int parent,lchild,rchild;)文档仅供参考,不当之处,请联系改正。HTNode;typedef HTNode*HuffmanTree;/*初始化一个哈夫曼树*/void InitHuffmanTree(HuffmaiiTree&HT,intm)(int i;HT=new HTNodem;for(i=0;im;i+)HTi.weight=O;HTi.parent=-l;HTi.lchild=-l;HTi.rchild=-l;)/a l*a KJA 1 t 从n个 结 点 中 选 取 最 小 的 两 个 结 点/1 a rj q、rj、rJw rj rj rjw rjw rjw rj rjw rJw rj rj rj rf rjw rj 乙、*Jw rjw rj rj rjw r j*rj rj r j f rj rj rjwvoid SelectMin(HuffmanTree&HT,int n,int&minl,int&min2)文档仅供参考,不当之处,请联系改正。typedef struct(int NewWeight;存储权intp;存储该结点所在的位置)TempNode,*TempTree;TempTree TT=new TempNoden;int i,j;j=0;for(i=0;in;i+)(if(HTi.parent=-l&HTi.weight!=O)(TTj.NewWeight=HTi.weight;TTj.p=i;j+;)将H T中没有双亲的结点存储到T T中int ml,m2;ml=m2=0;for(i=0;ij;i+)文档仅供参考,不当之处,请联系改正。if(TTi.NewWeightTTml.NewWeight)/此处不让取到相等,是因为结点中有相同权值的时候,m l取最前的那个。ml=i;)for(i=0;ij;i+)(if(ml=m2)m2+;当m l在第一个位置的时候,m2向后移一位if(TTi.NewWeight=TTm2.NewWeight&i!=ml)此处取到相等,是让在结点中有相同的权值的时候,m 2取最后的那个。m2=i;)minl=TTml.p;min2=TTm2.p;/*创立哈夫曼树*/void CreateHaffmanTree(HuffmanTree&HT,intn)(int i;int m;文档仅供参考,不当之处,请联系改正。int minl9min2;if(n=l)cout*Parameter Error!n;m=2*n-l;哈夫曼树中结点的个数InitHuffmanTree(HT,m);for(i=0;in;i+)(cinHTi.weight;)for(i=n;im;i+)(SelectMin(HT,i,minl,min2);HTminl.parent=i;HTmin2.parent=i;HTi.lchild=minl;HTi.rchild=min2;HTi.weight=HTminl.weight+HTmin2.weight;coutm inln nmin2endl;)文档仅供参考,不当之处,请联系改正。哈 夫 曼 编 码/芋 7*,*1*7,*1*!*K!*KJ/*/*KJ*哈夫曼编码的定义*/typedef struct(char ch;char bits10;)CodeNode;typedef CodeNode*HuffmanCode;/*哈夫曼编码的构造*/void CreateHuffmanCode(HuffmanTree&HT,HuffmanCode&HC,int n)(int i;int start;int c;int p;char*cd;char q;HC=new CodeNoden;文档仅供参考,不当之处,请联系改正。cd=new charn;cdn-l=70,;for(i=0;i=0)(start;cdstart=(HTp.lchild=c)?,O*:T ;c=p;)strcpy(HCi.bits,&cdstart);)delete cd;/*哈夫曼编码的输出*/void OutputHuffmanCode(HuffmanCode&HC,int n)(int i;for(i=0;in;i+)文档仅供参考,不当之处,请联系改正。coutH Ci.chn nHCi.bitsendl;)void main()(int i;coutvv”输入字符个数:”;cini;HuffmanTree HT;HuffmanCode HC;CreateHaffmanTree(HT,i);CreateHuffmanCode(HT,HC,i);OutputHuffmanCode(HC,i);)图形着色1:#include#include viostream回溯法using namespace std;/judge the colorationisValid or not.bool isValid(bool b55,int k,int x)文档仅供参考,不当之处,请联系改正。for(int i=0;ik;+i)if(!bki)continue;else if(bki&xk=xi)return false;return true;/by:潘乔木int main(int argc,char*argv)(int n=5;血 1 1 1 =3;第 个顶点的着色号码(解向量)int x5;bool b55=true,true,true,false,false,true,true,true,true,true,true,true,true,false,true,false,true,false,true,true,false,true,true,true,true;for(int i=0;i=0)文档仅供参考,不当之处,请联系改正。xk=xk+1;着色无效继续再当前层搜索有效的颜色while(xk=m&!isValid(b,k,x)xk=xk+1;if(xk=m)(if(k=n-l)break;/successelse为下一个结点着色k=k+1;)else 返回至上一层xk=0;k=k-1;)/printcout Five vertexes*coloration scheme is:endl;for(intj=0;j5;j+)cout xj n n;cout endl;文档仅供参考,不当之处,请联系改正。system(nPAUSEn);return EXIT SUCCESS;)0背包回溯法1:#includeusing namespace std;templateclass Knap(friend TypepKnapsack(Typep*,Typew*,Typew,int);private:Typep Bound(int i);void Backtrack(int i);Typew c;int n;Typew*w;Typep*p;Typew cw;Typep cp;Typep bestp;文档仅供参考,不当之处,请联系改正。);templateTypep Knap:Bound(int i)(Typew cleft=c-cw;Typep b=cp;while(i=n&wi=cleft)(cleft-=wi;b+=pi;i+;)if(i=n)b+=pi/wi*cleft;return b;)templatevoid Knap:Backtrack(int i)(if(in)(bestp=cp;return;文档仅供参考,不当之处,请联系改正。)if(cw+wibestp)Backtrack(i+1);)class Object(friend int Knapsack(int*,mtpublic:int operator=a.d);)private:int ID;文档仅供参考,不当之处,请联系改正。float d;template(Typep W=0;Typep P=0;Object*Q=new Objectn;for(int i=l;i=n;i+)|Qi-l.ID=i;Qi-l.d=1.0*pi/wi;P+=pi;W+=wi;)if(W=c)return P;sort(Q,n);KnapK;K.p=new Typepn+1;K.w=new Typewn+1;for(int i=l;i=n;i+)K.pi=pQi-l.ID;文档仅供参考,不当之处,请联系改正。K.wi=wQi-l.ID;)K.cp=O;K.cw=O;K.c=c;K.n=n;K.bestp=O;K.Backtrack(l);deleteQ;deleteK.w;deleteK.p;return K.bestp;);int mainO(int p=0,4,3,5,6,3);int w=0,3,5,6,2,7);int*pl=p;int*wl=w;int c=10,n=5;int bestx6;int x=Knapsack(p 1,wl,c,n);文档仅供参考,不当之处,请联系改正。cout nbest=c en d l;for(int i=0;i6,i+)(coutbestxi;)coutnResultn;return 0;)0-1背包动态规划法:#include#include#includeusing namespace std;int max(int i,int j)(return ij?i:j;)int min(int i,int j)(return ij?i:j;)文档仅供参考,不当之处,请联系改正。templatevoid Knapsack(Type*v,int*w,int c,int n,Type*m)(/0-1背包算法int jMax=min(wii-l,c);for(int j=O;j=jMax;j+)(mnj=0;)for(int t=wn;tl;i-)(jMax=min(wi-l,c);for(int j=O;j=jMax;j+)for(int k=wi;j=wl)mlc=max(mlc,m2c-wl+vl);)templatevoid TraceBack(Type*m,int*w,int c,int n,int*x)计算0 4背包路径for(int i=l;in;i+)(if(mic=mi+lc)xi=0;else(xi=l;c-=wi;)xn=(mnc)?l:0;)文档仅供参考,不当之处,请联系改正。void main()(/物品量n,承载重量c,各物体重量:w,各物体价值:vm 用来存储动态规划的子结构值int n,c,i;int*w,*v,*x;int*m;ifstream input(*Input.txtH,ios:in);ofstream output(nOutput.txtn,ios:out);if(!input|!output)(cerrnFi!e open or create error!nendl;exit(O);)else(inputnc;w=new intn+l;v=new intn+l;x=new intn+l;m=new int*n+l;for(i=0;i=n;i+)文档仅供参考,不当之处,请联系改正。mi=new intc+l;memset(mi,0,(c+l)*sizeof(int);for(i=l;i=n;i+)inputwi;for(i=l;i=n;i+)inputvi;input.closeQ;for(i=0;in+l;i+)for(int j=O;jc+l;j+)mij=0;Knapsack(v,w,c,n,m);for(i=0;i=n;i+)(for(int k=0;k=c;k+)outputmikn H;outputendl;)outputendl;TraceBack(m,w,c,n,x);for(i=l;i=n;i+)文档仅供参考,不当之处,请联系改正。outputxinoutput,close。;回收内存for(i=0;i=n;i+)delete mi;delete w;delete v;delete x;delete m;)0背包回溯法2:#include using namespace std;void input(int number,int.weight,int*price)(int i,n;coutvv”包的容量:H;cinweight0;coutvv”物品数:H;cin*number;coutvv”各物品的重量:n;文档仅供参考,不当之处,请联系改正。for(i=l;i=*number;i+)(cinweighti;)coutvv”各物品的价值:n;for(i=l;in)(if(*nowWeight*maxPrice)/coutnmaxPrice is文档仅供参考,不当之处,请联系改正。*maxPrice1 1 nowPrice isn*nowPriceendlendl;*maxPrice=*nowPrice;flag0=0;for(i=l;i=n;i+)(if(xi)|/coutvv”深 度 是:”W t v v i 是niendlendl;flag+flagO=i;)return;)else(for(i=0;i=l;i+)(xt=i;*nowWeight+=weightt*i;文档仅供参考,不当之处,请联系改正。*nowPrice+=pricet*i;/cout1 1 nowPrice is n*nowPricen深 度 是:n t n xn t n是:niendl;backtrack(t+l,n,weight,price,maxPrice,flag,now Weight,nowPrice,x);*nowWeight-=weightt*i;*nowPrice-=pricet*i;)void output(int*maxPrice,int*flag)(int i;coutendl;cout最大价值:H;cout*maxPriceendl;cout”所选物品为:;for(i=l;i=flag0;i+)(coutflagin n;)文档仅供参考,不当之处,请联系改正。coutendl;)int main()(int templ=0,temp2=-l,temp3=0,temp4=0;这一行很重要!!int*number=&templ,weight100,price100;int*maxPrice=&temp2,flag100,*nowWeight=&temp3,*nowPrice=&temp4,x100;input(number,weight,price);backtrack(l,*number,weight,price,maxPrice,flag,nowWeight,nowPrice,x);output(maxPrice,flag);return 0;)最小生成树prim法1:#include#includeusing namespace std;struct MGraph文档仅供参考,不当之处,请联系改正。void createGraph(int n,int m);int Prim(int u);int mat100100;/弧的信息int vexnum;/顶点数int MinVertex(int dist,bool visited););int MGraph:Prim(int u)从顶点u 出发构造网的从顶点最小生成树int adjvex100;int dist100;bool visited100;int i;/辅助数组初始化for(i=0;ivexnum;i+)(adjvexi=u;disti=matui;)fill(visited,visited+vexnum,false);visitedu=true;/初始,U=u 初始,=for(int v=l;v=vexnum-l;v+)文档仅供参考,不当之处,请联系改正。/选 择 vexnum-1个顶点 边)个顶点(边int k=MinVertex(dist,visited);/加入生成树的下一个顶点(k)visitedk=true;新 节 点 加 入 集 合U/调整集合V-U 中剩余顶点到集合U 的最短距离for(i=0;ivexnum;i+)(if(visitedi=false&matkidisti)(adj vexi=k;disti=matki;)int cost=0;distu=0;for(i=0;ivexnum;i+)文档仅供参考,不当之处,请联系改正。cost+=disti;)return cost;/寻 找V-U中未被访问,且d ist最小的顶点中未被访问,int MGraph:MinVertex(int dist,bool visited)(int k=-1;int min=INT MAX;for(int i=0;ivexnum;i+)(if(disti min&visitedi=false)(min=disti;k=i;)return k;)void MGraph:createGraph(int n,int m)vexnum=n;文档仅供参考,不当之处,请联系改正。for(int v=0;vvexnum;v+)(for(int w=0;wvexnum;w+)(matvw=INT_MAX;)for(int i=0;im;i+)(int a,b,c;cin ab c;matab=c;matba=c;)int main。/个 顶 点(边 选 择vexnum 个顶点