欢迎来到天天文库
浏览记录
ID:57222133
大小:79.16 KB
页数:6页
时间:2020-08-06
《管道铺设施工的最佳方案-----------完整程序代码讲课讲稿.docx》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、管道铺设施工的最佳方案-----------完整程序代码精品文档1)内容:需要在某个城市n个居民小区之间铺设煤气管道,则在这n个居民小区之间只需要铺设n-1条管道即可。假设任意两个小区之间都可以铺设管道,但由于地理环境不同,所需要的费用也不尽相同。选择最优的方案能使总投资尽可能小,这个问题即为求无向网的最小生成树。2)要求:在可能假设的m条管道中,选取n-1条管道,使得既能连通n个小区,又能使总投资最小。每条管道的费用以网中该边的权值形式给出,网的存储采用邻接表的结构。3)测试数据:使用下图给出
2、的无线网数据作为程序的输入,求出最佳铺设方案。右侧是给出的参考解。4)输入输出:参考完整代码:#include"iostream"#include"stdlib.h"#defineMAX_VERTEX_NUM20typedeffloatWeightType;typedefstructArcNode{intadjvex;WeightTypeweight;structArcNode*nextarc;}ArcNode;收集于网络,如有侵权请联系管理员删除精品文档typedefstructVertexN
3、ode{chardata;ArcNode*firstarc;}VertexNode,AdjList[MAX_VERTEX_NUM];typedefstruct{AdjListvertices;intvexnum,arcnum;intkind;}ALGraph;intLocateVex(ALGraphG,charv){inti;for(i=0;i4、Graph&G){inti,j,k;charvi,vj;WeightTypeweight;ArcNode*p,*q;std::cout<<"请输入顶点个数,边数和图的类型:";std::cin>>G.vexnum>>G.arcnum>>G.kind;for(i=0;i>G.vertices[i].data;G.vertices[i].firstarc=NULL;}for(k=0;k5、m;k++){std::cout<<"请输入两顶点和其边的权值:";std::cin>>vi>>vj>>weight;i=LocateVex(G,vi);j=LocateVex(G,vj);p=(ArcNode*)malloc(sizeof(ArcNode));p->adjvex=j;收集于网络,如有侵权请联系管理员删除精品文档p->weight=weight;p->nextarc=G.vertices[i].firstarc;G.vertices[i].firstarc=p;if(G.ki6、nd==2){q=(ArcNode*)malloc(sizeof(ArcNode));q->adjvex=i;q->weight=p->weight;q->nextarc=G.vertices[j].firstarc;G.vertices[j].firstarc=q;}}}intMinEdge(WeightTypelowcost[],intvexmun){inti,k;WeightTypej;k=0;while(lowcost[k]==0){k++;}j=lowcost[k];for(i=k+17、;i8、=G.vertices[v0].firstarc;while(p){lowcost[G,p->adjvex]=p->weight;p=p->nextarc;}lowcost[v0]=0;for(i=0;i=G.vexnum)return;std::cout<<"("<
4、Graph&G){inti,j,k;charvi,vj;WeightTypeweight;ArcNode*p,*q;std::cout<<"请输入顶点个数,边数和图的类型:";std::cin>>G.vexnum>>G.arcnum>>G.kind;for(i=0;i>G.vertices[i].data;G.vertices[i].firstarc=NULL;}for(k=0;k5、m;k++){std::cout<<"请输入两顶点和其边的权值:";std::cin>>vi>>vj>>weight;i=LocateVex(G,vi);j=LocateVex(G,vj);p=(ArcNode*)malloc(sizeof(ArcNode));p->adjvex=j;收集于网络,如有侵权请联系管理员删除精品文档p->weight=weight;p->nextarc=G.vertices[i].firstarc;G.vertices[i].firstarc=p;if(G.ki6、nd==2){q=(ArcNode*)malloc(sizeof(ArcNode));q->adjvex=i;q->weight=p->weight;q->nextarc=G.vertices[j].firstarc;G.vertices[j].firstarc=q;}}}intMinEdge(WeightTypelowcost[],intvexmun){inti,k;WeightTypej;k=0;while(lowcost[k]==0){k++;}j=lowcost[k];for(i=k+17、;i8、=G.vertices[v0].firstarc;while(p){lowcost[G,p->adjvex]=p->weight;p=p->nextarc;}lowcost[v0]=0;for(i=0;i=G.vexnum)return;std::cout<<"("<
5、m;k++){std::cout<<"请输入两顶点和其边的权值:";std::cin>>vi>>vj>>weight;i=LocateVex(G,vi);j=LocateVex(G,vj);p=(ArcNode*)malloc(sizeof(ArcNode));p->adjvex=j;收集于网络,如有侵权请联系管理员删除精品文档p->weight=weight;p->nextarc=G.vertices[i].firstarc;G.vertices[i].firstarc=p;if(G.ki
6、nd==2){q=(ArcNode*)malloc(sizeof(ArcNode));q->adjvex=i;q->weight=p->weight;q->nextarc=G.vertices[j].firstarc;G.vertices[j].firstarc=q;}}}intMinEdge(WeightTypelowcost[],intvexmun){inti,k;WeightTypej;k=0;while(lowcost[k]==0){k++;}j=lowcost[k];for(i=k+1
7、;i8、=G.vertices[v0].firstarc;while(p){lowcost[G,p->adjvex]=p->weight;p=p->nextarc;}lowcost[v0]=0;for(i=0;i=G.vexnum)return;std::cout<<"("<
8、=G.vertices[v0].firstarc;while(p){lowcost[G,p->adjvex]=p->weight;p=p->nextarc;}lowcost[v0]=0;for(i=0;i=G.vexnum)return;std::cout<<"("<
此文档下载收益归作者所有