6 xiasha westlake xiasha station 60 xiasha ShoppingCenterofHangZhou 30 station westlake 20 ShoppingCenterofHangZhou supermarket 10 xiasha supermarket 50 supermarket westlake 10 -1
50 Hint: The best route is: xiasha->ShoppingCenterofHangZhou->supermarket->westlake 虽然偶尔会迷路,但是因为有了你的帮助 **和**从此还是过上了幸福的生活。 ――全剧终――
代码:
#include<stdio.h> #include<string.h> #include<stdlib.h> #define inf 0xffffff char s1[35]; char s2[35]; int mp[222][222]; int dis[222]; int visit[222]; char s[222][40]; int count; int suan(int u,int v) { memset(visit,0,sizeof(visit)); for(int i=1; i<=count ; i++) { dis[i]=mp[u][i]; } dis[u]=0; visit[u]=1; for(int i=1; i<=count; i++) { int pos=u; int mincost=inf; for(int j=1; j<=count; j++) { if(!visit[j]&&dis[j]<mincost) { pos=j; mincost=dis[j]; } } visit[pos]=1; for(int j=1; j<=count; j++) { if(!visit[j]&&dis[j]>dis[pos]+mp[pos][j]) dis[j]=dis[pos]+mp[pos][j]; } } if(dis[v]==inf) return -1; else return dis[v]; } int main() { int n; while(scanf("%d",&n)!=EOF) { if(n==-1) break; for(int i=1; i<200; i++) { for(int j=1; j<200; j++) { if(i==j) mp[i][j]=0; else mp[i][j]=inf; } } scanf("%s%s",&s[1],&s[2]); count=2;int flag; int a,b,c; while(n--) { scanf("%s%s%d",&s1,&s2,&c); flag=0; for(int i=1; i<=count; i++) { if(strcmp(s1,s[i])==0) { flag=1; a=i; break; } } if(flag==0) { strcpy(s[++count],s1); a=count; } flag=0; for(int i=1; i<=count; i++) { if(strcmp(s2,s[i])==0) { flag=1; b=i; break; } } if(flag==0) { strcpy(s[++count],s2); b=count; } if(mp[a][b]>c) mp[a][b]=mp[b][a]=c; } if(strcmp(s[1],s[2])==0) printf("0\n"); else printf("%d\n",suan(1,2)); } }