标签:
1 #include<map> 2 #include<iostream> 3 #include<string> 4 #include<cstdio> 5 #include<cstring> 6 #include<algorithm> 7 using namespace std; 8 9 const int inf=0x3f3f3f3f; 10 int G[155][155],vis[155],d[155]; 11 map<string,int>edges; 12 int t,n; 13 14 int dijkstra(int S,int T) 15 { 16 int u,i,j; 17 memset(vis,0,sizeof(vis)); 18 //memset(d,0,sizeof(d)); 19 for(i=0;i<=t;i++) 20 d[i]=G[S][i]; 21 d[S]=0; 22 vis[S]=1; 23 for(i=1;i<n;i++) 24 { 25 int temp=inf; 26 for(j=0;j<=t;j++) 27 { 28 if(!vis[j]&&d[j]<temp) 29 { 30 temp=d[j]; 31 u=j; 32 } 33 } 34 if(temp==inf) 35 continue; 36 vis[u]=true; 37 for(j=0;j<=t;j++) 38 { 39 if(!vis[j]&&d[u]+G[u][j]<d[j]) 40 d[j]=d[u]+G[u][j]; 41 } 42 } 43 return d[T]; 44 } 45 46 int main() 47 { 48 int i,S,T,num; 49 string s1,s2; 50 // freopen("in.txt","r",stdin); 51 while(scanf("%d",&n)&&n!=-1) 52 { 53 t=-1; 54 edges.clear(); 55 memset(G,inf,sizeof(G)); 56 cin>>s1>>s2; 57 edges[s1]=++t; 58 S=t; 59 if(!edges.count(s2)) 60 edges[s2]=++t; 61 T=t; 62 for(i=0;i<n;i++) 63 { 64 cin>>s1>>s2>>num; 65 if(!edges.count(s1)) 66 edges[s1]=++t; 67 if(!edges.count(s2)) 68 edges[s2]=++t; 69 G[edges[s2]][edges[s1]]=G[edges[s1]][edges[s2]]=min(num,G[edges[s1]][edges[s2]]); 70 } 71 int ans=dijkstra(S,T); 72 if(ans==inf) 73 printf("-1\n"); 74 else 75 printf("%d\n",ans); 76 } 77 return 0; 78 }
HDU 2122 HDU Today(dijkstra+map)
标签:
原文地址:http://www.cnblogs.com/homura/p/4722361.html