aj30

Untitled

Mar 30th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. map<string,int> id;
  5. vector<pair<string,int>> v[106];
  6. int main() {
  7. ios_base::sync_with_stdio(false);cin.tie(NULL);
  8. string start, end;
  9. cin >> start>>end;
  10. int x = 1;
  11. // map<int,string> str;
  12. // str[x] = start;
  13. id[start] = x++;
  14. // str[x] = end;
  15. id[end] = x++;
  16.  
  17.  
  18. string a,b;int cost;
  19. while(cin>>a>>b>>cost){
  20. if(id[a]==0){/*str[x]=a;*/id[a] = x++;}
  21. if(id[b]==0){/*str[x]=b;*/id[b]=x++;}
  22. v[id[a]].push_back({b,cost});
  23. }
  24. // dfs(1,-1);
  25. int flg = 0;
  26. priority_queue<pair<int,pair<int,pair<string,string>>>, vector<pair<int,pair<int,pair<string,string>>>>, greater<pair<int,pair<int,pair<string,string>>>> > q;
  27. q.push({0,{0,{start,start}}});
  28. // vector<pair<int,pair<int,string>>> ans;//dist, no. string
  29. // cout<<start<<endl;
  30. while(!q.empty()){
  31. pair<int,pair<int,pair<string,string>>> p = q.top();
  32. q.pop();
  33. // distance, no.of.cities.explored.till_now., entire.path.in.string, last.city.name
  34. // pair<int,pair<int,pair<string,string>>>
  35.  
  36. if(p.second.second.second==end){
  37. // ans.push_back({p.first,{p.second.first,p.second.second.first}});
  38. flg = 1;
  39. cout<<p.second.second.first<<' '<<p.first<<endl;
  40. }
  41. for(auto &neigh: v[id[p.second.second.second]]){
  42. int c = p.first+neigh.second;
  43. q.push({c,{p.second.first+1,{p.second.second.first+" "+neigh.first,neigh.first }}});
  44. }
  45. }
  46. if(flg==0){
  47. cout<<"No Trains";
  48. }
  49. return 0;
  50. }
Add Comment
Please, Sign In to add comment