欢迎来到天天文库
浏览记录
ID:47064892
大小:33.33 KB
页数:14页
时间:2019-07-12
《图地最小生成树和最短路径》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、标准文档采用图的邻接矩阵方式查找图的最小生成树和最短路径的完整程序代码:#include#include#include#include#include#include#include#include#includeusingnamespacestd;#defineMAXNUM26#defineMAXVALUE32767//矩?阵¨®领¢¨¬接¨®图ª?的Ì?定¡§义°?typede
2、fstruct{charVertex[MAXNUM];//vertexintEdges[MAXNUM][MAXNUM];//edgeintIstrave[MAXNUM];//flagintVertex_num;//numberofvertexintEdges_num;//numberofedgesintGlaphtype;//0:nodirection;1:direction}Maxtrixgraph;//创ä¡ä建¡§矩?阵¨®领¢¨¬接¨®矩?阵¨®voidCreatmatrixgraph(Maxtrixgraph*G){int
3、i,j,k,weight;实用文案标准文档charstart,end;cout<<"pleaseinputtheallvertexinformation:"<Vertex_num;i++){cout<<"the"<>G->Vertex[i];}cout<Edges_num;k++){cout<<"the"<>
4、start>>end>>weight;i=0;j=0;while(G->Vertex[i]!=start){i++;}while(G->Vertex[j]!=end){j++;}G->Edges[i][j]=weight;if(G->Glaphtype==0)G->Edges[j][i]=weight;}}//输º?出?邻¢¨²接¨®矩?阵¨®实用文案标准文档voidOutmatrix(Maxtrixgraph*G){inti,j;cout<<"Thevertexisfollow:"<Verte
5、x_num;j++)cout<<""<Vertex[j];cout<Vertex_num;i++){cout<Vertex[i];for(j=0;jVertex_num;j++){if(G->Edges[i][j]==MAXVALUE)cout<<"#";elsecout<<""<Edges[i][j];}cout<6、建¡§方¤?法¤¡§typedefstructedgenode{intVertex;//vertexnumberintweight;//quan实用文案标准文档structedgenode*next;//thenextedgenode}EdgeNode;//图ª?的Ì?邻¢¨²接¨®链¢¡ä表À¨ª定¡§义°?typedefstruct{EdgeNode*Adjlist[MAXNUM];intvertex_num;intedge_num;intGraphtype;}ListGraph;//生¦¨²成¨¦图ª?的Ì?邻¢¨²接¨®表À7、¨ªvoidCreatgraph(ListGraph*G){inti,weight,start,end;EdgeNode*s;for(i=0;ivertex_num;i++)//clearthenodepointerG->Adjlist[i]=NULL;for(i=0;iedge_num;i++)//inputthe2nodeofeachedgeandweight{cout<<"the"<>start>>end>>weight;s=(EdgeNode*)malloc(sizeof(8、EdgeNode));s->next=G->Adjlist[start];s->Vertex=end;s->weight=weight;G->Adjlist[start]=s;if(G->Graphtype==0)实用文案标准文档{s
6、建¡§方¤?法¤¡§typedefstructedgenode{intVertex;//vertexnumberintweight;//quan实用文案标准文档structedgenode*next;//thenextedgenode}EdgeNode;//图ª?的Ì?邻¢¨²接¨®链¢¡ä表À¨ª定¡§义°?typedefstruct{EdgeNode*Adjlist[MAXNUM];intvertex_num;intedge_num;intGraphtype;}ListGraph;//生¦¨²成¨¦图ª?的Ì?邻¢¨²接¨®表À
7、¨ªvoidCreatgraph(ListGraph*G){inti,weight,start,end;EdgeNode*s;for(i=0;ivertex_num;i++)//clearthenodepointerG->Adjlist[i]=NULL;for(i=0;iedge_num;i++)//inputthe2nodeofeachedgeandweight{cout<<"the"<>start>>end>>weight;s=(EdgeNode*)malloc(sizeof(
8、EdgeNode));s->next=G->Adjlist[start];s->Vertex=end;s->weight=weight;G->Adjlist[start]=s;if(G->Graphtype==0)实用文案标准文档{s
此文档下载收益归作者所有