标签:
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19552 Accepted Submission(s):
4588
1 #include <map> 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 map<string, int> mmap; 7 int pic[1550][1550], dis[1550], vis[1550]; 8 const int INF = 0x3f3f3f3f; 9 int Dijkstra(int src, int end, int k) 10 { 11 memset(vis, 0, sizeof(vis)); 12 for(int i = 1; i < k; i++) 13 dis[i] = pic[src][i]; 14 vis[src] = 1; 15 for(int i = 1; i < k - 1; i++) 16 { 17 int temp, min = INF; 18 for(int j = 1; j < k; j++) 19 { 20 if(!vis[j]&&dis[j]<min) 21 { 22 min = dis[j]; 23 temp = j; 24 // printf("%d\n", temp); 25 } 26 } 27 vis[temp] = 1; 28 for(int j = 1; j < k; j++) 29 if(!vis[j] && dis[j] > dis[temp] + pic[temp][j]) 30 dis[j] = dis[temp] + pic[temp][j]; 31 32 } 33 return dis[end]==INF?-1:dis[end]; 34 } 35 int main() 36 { 37 int t, k, sta, end;; 38 while(~scanf("%d", &t), t != -1) 39 { 40 for(int i = 1; i < 155; i++) 41 for(int j = 1; j < 155; j++) 42 pic[i][j]=(i==j?0:INF); 43 k = 1; mmap.clear(); 44 char a[110], b[110]; int c; // map存东西时要从1开始, important; 45 cin >> a >> b; 46 if(!mmap[a]) mmap[a] = k++; 47 sta = k-1; 48 if(!mmap[b]) mmap[b] = k++; 49 end = k-1; 50 // printf("%d %d", mmap[a], mmap[b]); 51 for(int i = 0; i < t; i++) 52 { 53 cin >> a >> b >> c; 54 if(!mmap[a]) mmap[a] = k++; 55 if(!mmap[b]) mmap[b] = k++; 56 if(pic[mmap[a]][mmap[b]] > c) 57 pic[mmap[a]][mmap[b]] = pic[mmap[b]][mmap[a]] = c; 58 } 59 printf("%d\n", Dijkstra(sta, end, k)); 60 } 61 return 0; 62 }
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4738015.html