Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include<iostream>
  2. #include<map>
  3. #include<vector>
  4. #include<cstdio>
  5. #include<string>
  6. #include<queue>
  7.  
  8. using namespace std;
  9.  
  10. const int INF = 1 << 30;
  11. const int MAX = (int)1e5 + 1;
  12.  
  13. map< string, int > M;
  14. map< string, int > :: iterator it;
  15.  
  16. vector < pair < int , int  > > graph[MAX];
  17.  
  18. int dist[ MAX ];
  19. int used[ MAX ];
  20.  
  21. int main() {
  22.  
  23.     char name[20];
  24.     int T;
  25.     scanf("%d",&T);
  26.  
  27.     for(int t = 0; t < T; ++t) {
  28.  
  29.         int N;
  30.         scanf("%d",&N);
  31.  
  32.         for(int i = 1; i <= N; ++i) {
  33.             int cityCount;
  34.             scanf("%s%d",name,&cityCount);
  35.             M.insert( pair < string, int >(name,i));
  36.             for(int j = 1; j <= cityCount; ++j) {
  37.                     int to;
  38.                     int weight;
  39.                 scanf("%d%d",&to,&weight);
  40.                 graph[i].push_back( pair< int, int >(weight,to));
  41.             }
  42.  
  43.         }
  44.         int q;
  45.         scanf("%d",&q);
  46.         for(int j = 0; j < q; ++j) {
  47.             scanf("%s",name);
  48.             it = M.find(name);
  49.             int from = (*it).second;
  50.             scanf("%s",name);
  51.             it = M.find(name);
  52.             int to = (*it).second;
  53.             priority_queue< pair < int, int >, vector< pair < int, int > >, greater < pair < int, int > > > PQ;
  54.  
  55.             PQ.push( pair < int, int >(0,from) );
  56.  
  57.             for(int i  = 1; i <= N; ++i ) {
  58.                 dist[i] = INF;
  59.                 used[i] = 0;
  60.             }
  61.             dist[from] = 0;
  62.  
  63.             while(!PQ.empty()) {
  64.                 int v = PQ.top().second;
  65.                 int w = PQ.top().first;
  66.                 PQ.pop();
  67.                 if(used[v]) continue;
  68.                 int currentSize = graph[v].size();
  69.                 for(int m = 0; m < currentSize; ++m) {
  70.                     int toV = graph[v][m].second;
  71.                     int toW = graph[v][m].first;
  72.  
  73.                     if(!used[toV] && dist[v] + toW < dist[toV]) {
  74.                         dist[toV] = dist[v] + toW;
  75.                         PQ.push( pair < int, int >(dist[toV],toV));
  76.                     }
  77.                 }
  78.                 used[v] = 1;
  79.                 if(v == to) break;
  80.             }
  81.             printf("%d\n",dist[to]);
  82.         }
  83.         for(int i = 1; i <= N; ++i) graph[i].clear();
  84.         M.clear();
  85.     }
  86.  
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement