hdu 2112 HDU Today

http://acm.hdu.edu.cn/showproblem.php?pid=2112

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <string>
 5 #include <map>
 6 #include <algorithm>
 7 #define maxn 6000
 8 using namespace std;
 9 const int inf=1<<28;
10 
11 char str1[500],str2[500],str3[500],str4[500];
12 int g[maxn][maxn];
13 int n;
14 int dis[maxn];
15 bool vis[maxn];
16 int ans;
17 void dijstra(int src)
18 {
19     memset(vis,false,sizeof(vis));
20     for(int i=0; i<ans; i++) dis[i]=inf;
21     dis[src]=0;
22     for(int i=1; i<=ans; i++)
23     {
24         int x,m=inf;
25         for(int y=1; y<ans; y++) if(!vis[y]&&dis[y]<m) m=dis[x=y];
26         vis[x]=true;
27         for(int y=1; y<ans; y++) dis[y]=min(dis[y],dis[x]+g[x][y]);
28     }
29 }
30 
31 
32 int main()
33 {
34     while(scanf("%d",&n)!=EOF)
35     {
36         if(n==-1)break;
37         for(int i=1; i<=200; i++)
38         {
39             for(int j=1; j<=200; j++)
40             {
41                 if(i==j) g[i][j]=0;
42                 else g[i][j]=inf;
43             }
44         }
45         
46         getchar();
47         map<string,int>q;
48         cin>>str1>>str2;
49         int t;
50         ans=1;
51         for(int i=1; i<=n; i++)
52         {
53             cin>>str3>>str4>>t;
54             if(q[str3]==0)
55                 q[str3]=ans++;
56             if(q[str4]==0)
57                 q[str4]=ans++;
58             //printf("%d %d
",q[str3],q[str4]);
59             g[q[str3]][q[str4]]=g[q[str4]][q[str3]]=min(g[q[str3]][q[str4]],t);
60         }
61         if(q[str1]==0) q[str1]=ans++;
62         if(q[str2]==0) q[str2]=ans++;
63         dijstra(q[str1]);
64         if(dis[q[str2]]==inf) printf("-1
");
65         else printf("%d
",dis[q[str2]]);
66     }
67     return 0;
68 }
View Code
原文地址:https://www.cnblogs.com/fanminghui/p/3731227.html