标签:poj poj2075 tangled in cables 最小生成树
100.0 4 Jones Smiths Howards Wangs 5 Jones Smiths 2.0 Jones Howards 4.2 Jones Wangs 6.7 Howards Wangs 4.0 Smiths Wangs 10.0
Need 10.2 miles of cable这题是最小树的变形,将各个名字转化为点,最小树求即可!当然还有长度要求!代码:#include<stdio.h> #include<string.h> #define INF 0xfffffff #define N 1010 char s[10000][30]; double map[N][N],cost[N],m,outcome; int country,road,v[N]; void prim() { int i,j,next; double mincost; memset(v,0,sizeof(v)); for(i=0;i<country;i++) { cost[i]=map[0][i]; } v[0]=1; for(i=1;i<country;i++)//寻找最短的距离 { mincost=INF; for(j=0;j<country;j++) { if(!v[j]&&mincost>cost[j]) { mincost=cost[j]; next=j; } } outcome+=mincost; v[next]=1; for(j=0;j<country;j++)//更新路径 { if(!v[j]&&cost[j]>map[next][j]) { cost[j]=map[next][j]; } } } } int main() { int i,j; double l; char a[30],b[30]; while(scanf("%lf",&m)!=EOF) { scanf("%d",&country); for(i=0;i< country;i++) scanf("%s",s[i]); scanf("%d",&road); for(i=0;i<country;i++) { for(j=0;j<country;j++) { if(i==j) map[i][j]=0; else map[i][j]=INF; } } for(i=0;i<road;i++)//字符转化为点 { scanf("%s%s%lf",a,b,&l); int k,t; for(j=0;j<country;j++) { if(strcmp(s[j],a)==0) k=j; } for(j=0;j<country;j++) { if(strcmp(s[j],b)==0) t=j; } map[t][k]=map[k][t]=l; } prim(); if(outcome<=m) printf("Need %.1f miles of cable\n",outcome); else printf("Not enough cable\n"); return 0; } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:poj poj2075 tangled in cables 最小生成树
原文地址:http://blog.csdn.net/zhangxiaoxiang123/article/details/47618099