fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int testcases;
  6. cin >> testcases;
  7. while(testcases-->0)
  8. {
  9. int cities;
  10. cin >> cities;
  11. string arr[cities+1];
  12. vector<pair<int,int>> adjacencylist[cities];
  13. unordered_map<string,int> mymap;
  14. for(int i=1;i<=cities;i++)
  15. {
  16. cin >> arr[i];
  17. mymap[arr[i]]=i;
  18. int temp;
  19. cin >> temp;
  20. while(temp-->0)
  21. {
  22. int nr,cost;
  23. cin >> nr >> cost;
  24. adjacencylist[i].push_back(pair<int,int>(cost,nr));
  25. }
  26. }
  27. int xx;
  28. cin >> xx;
  29. while(xx-->0)
  30. {
  31. string source,destination;
  32. cin >> source >> destination;
  33. int index1=mymap[source],index2=mymap[destination];
  34. int dist[cities+1];
  35. for(int i=1;i<=cities;i++)
  36. {
  37. dist[i]=10000;
  38. }
  39. dist[index1]=0;
  40. priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> myqueue;
  41. myqueue.push(pair<int,int>(dist[index1],index1));
  42. while(myqueue.size())
  43. {
  44. pair<int,int> p=myqueue.top();
  45. myqueue.pop();
  46. //cout << "extracted min " << p.second << " ans distance of it is " << p.first << endl;
  47. for(pair<int,int> vv : adjacencylist[p.second])
  48. {
  49. if(dist[vv.second]>dist[p.second]+vv.first)
  50. {
  51. dist[vv.second]=p.first+vv.first;
  52. // cout << "pushing " << vv.second << " vertex ans its distance is " << dist[vv.second] << endl;
  53. myqueue.push(pair<int,int>(dist[vv.second],vv.second));
  54. }
  55. }
  56. }
  57. cout << dist[index2] << endl;
  58. }
  59.  
  60. }
  61. }
  62.  
Runtime error #stdin #stdout 0s 3472KB
stdin
1
4
gdansk
2
2 1
3 3
bydgoszcz
3
1 1
3 1
4 4
torun
3
1 3
2 1
4 1
warszawa
2
2 4
3 1
2
gdansk warszawa
bydgoszcz warszawa
stdout
Standard output is empty