标签:des style blog color os io 数据 for
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 <iostream> #include <cstring> #include <cstdio> #include <map> #include <algorithm> #define MX 151 #define MAX 1000000 using namespace std; int m[155][155]; int n,p[155],num[155]; void distance(int n) { memset(p,0,sizeof(p)); for(int i=1;i<=n;i++) { num[i]=m[1][i]; } p[1]=1; num[1]=0; for(int i=1;i<=n;i++) { int k,min=MAX; for(int j=1;j<=n;j++) if(!p[j]&&num[j]<min) { min=num[j]; k=j; } if(min==MAX)break; p[k]=1; for(int j=1;j<=n;j++) { if(!p[j]&&num[j]>num[k]+m[k][j]) num[j]=num[k]+m[k][j]; } } if(num[2]!=MAX)printf("%d\n",num[2]); else printf("-1\n"); } int main() { char s1[50], s2[50]; int t; map<string,int>mp; while (~scanf("%d",&n)&&n!=-1){ mp.clear(); for(int i=1;i<=MX;i++) for(int j=1;j<=MX;j++) m[i][j]=MAX; //cin >> s1 >> s2; scanf("%s%s",&s1,&s2); mp[s1]=1; mp[s2]=2; int cont=3,flag=0; if(strcmp(s1,s2)==0)flag=1; for(int i=0;i<n;i++) { scanf("%s%s%d",&s1,&s2,&t); if(!mp[s1])mp[s1]=cont++; if(!mp[s2])mp[s2]=cont++; m[mp[s1]][mp[s2]]=m[mp[s2]][mp[s1]]=t; } if(flag) { printf("0\n");continue; } else { distance(cont); } } return 0; }
标签:des style blog color os io 数据 for
原文地址:http://blog.csdn.net/zhangweiacm/article/details/38517667