标签:
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19680 Accepted Submission(s): 4632
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<map> 5 #include<string> 6 #define MAX 0x3f3f3f3f 7 using namespace std; 8 int ma[160][160]; 9 int d[160],n,t; 10 void dijkstra() 11 { 12 int i,j,min,mark,used[160]; 13 for(i=1;i<=t;i++) 14 { 15 used[i]=0; 16 d[i]=ma[1][i]; 17 } 18 d[1]=0; 19 used[1]=1; 20 for(i=1;i<=t;i++) 21 { 22 mark=-1; 23 min=MAX; 24 for(j=1;j<=t;j++) 25 { 26 if(!used[j]&&d[j]<min) 27 { 28 min=d[j]; 29 mark=j; 30 } 31 } 32 if(mark==-1) 33 break; 34 used[mark]=1; 35 for(j=1;j<=t;j++) 36 { 37 if(!used[j]&&d[j]>d[mark]+ma[mark][j]) 38 d[j]=d[mark]+ma[mark][j]; 39 } 40 } 41 } 42 43 44 45 int main() 46 { 47 int i; 48 char s1[160],s2[160]; 49 map<string,int> q; 50 while(scanf("%d",&n),n!=-1) 51 { 52 memset(ma,MAX,sizeof(ma)); 53 q.clear(); 54 scanf("%s%s",s1,s2); 55 q[s1]=1; 56 q[s2]=2; 57 int flag=0,a; 58 if(strcmp(s1,s2)==0) 59 flag=1;//起点终点相同标记下,一会输出060 t=3; 61 for(i=0;i<n;i++) 62 { 63 scanf("%s%s%d",s1,s2,&a); 64 if(!q[s1]) q[s1]=t++; 65 if(!q[s2]) q[s2]=t++;//转化为数字过程 66 ma[q[s1]][q[s2]]=ma[q[s2]][q[s1]]=a; 67 } 68 dijkstra(); 69 if(flag) 70 printf("0\n"); 71 else if(d[2]==MAX) 72 printf("-1\n"); 73 else 74 printf("%d\n",d[2]); 75 } 76 return 0; 77 }
标签:
原文地址:http://www.cnblogs.com/Eric-keke/p/4739902.html