Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- map<string,int> id;
- vector<pair<string,int>> v[106];
- int main() {
- ios_base::sync_with_stdio(false);cin.tie(NULL);
- string start, end;
- cin >> start>>end;
- int x = 1;
- // map<int,string> str;
- // str[x] = start;
- id[start] = x++;
- // str[x] = end;
- id[end] = x++;
- string a,b;int cost;
- while(cin>>a>>b>>cost){
- if(id[a]==0){/*str[x]=a;*/id[a] = x++;}
- if(id[b]==0){/*str[x]=b;*/id[b]=x++;}
- v[id[a]].push_back({b,cost});
- }
- // dfs(1,-1);
- int flg = 0;
- 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;
- q.push({0,{0,{start,start}}});
- // vector<pair<int,pair<int,string>>> ans;//dist, no. string
- // cout<<start<<endl;
- while(!q.empty()){
- pair<int,pair<int,pair<string,string>>> p = q.top();
- q.pop();
- // distance, no.of.cities.explored.till_now., entire.path.in.string, last.city.name
- // pair<int,pair<int,pair<string,string>>>
- if(p.second.second.second==end){
- // ans.push_back({p.first,{p.second.first,p.second.second.first}});
- flg = 1;
- cout<<p.second.second.first<<' '<<p.first<<endl;
- }
- for(auto &neigh: v[id[p.second.second.second]]){
- int c = p.first+neigh.second;
- q.push({c,{p.second.first+1,{p.second.second.first+" "+neigh.first,neigh.first }}});
- }
- }
- if(flg==0){
- cout<<"No Trains";
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment