Advertisement
iPsych0

UVa 762 :: ACCEPTED

Feb 9th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. //RE is not mightier than me
  2.  
  3. #include<bits/stdc++.h>
  4. #define mx 10009
  5. using namespace std;
  6.  
  7. vector<int> G[mx];
  8. int cr=0;
  9.  
  10.  
  11. void clean(int cr) {
  12.     for(int i=0; i<=cr; i++)
  13.         G[i].clear();
  14.     return;
  15. }
  16.  
  17. int bfs(string src, string dst, map<string,int> mp, string *id, bool *visited, int *parent) {
  18.  
  19.     int nodeLevel[cr];
  20.     memset(nodeLevel, 0, sizeof nodeLevel);
  21.  
  22.     if(src==dst){
  23.         cout<<src<<" "<<dst<<endl;
  24.         return 0;
  25.     }
  26.  
  27.     //cout<<"Source and destination is not equal"<<endl;
  28.  
  29.     queue<int> sack;
  30.     sack.push(mp[src]);
  31.     visited[mp[src]]= true;
  32.     nodeLevel[mp[src]]=0;
  33.  
  34.     while(!sack.empty()) {
  35.         int top= sack.front();
  36. //        cout<<"Top found is: "<<id[top]<<endl;
  37.         sack.pop();
  38.  
  39.         for(int i=0; i<G[top].size(); i++) {
  40.  
  41.             if(!visited[G[top][i]]) {
  42. //                cout<<"Dealing with: "<<id[G[top][i]]<<endl;
  43.                 nodeLevel[G[top][i]]= nodeLevel[top] + 1;
  44. //                cout<<"Node Level of "<<id[G[top][i]]<<" is: "<<nodeLevel[G[top][i]]<<endl;
  45.                 visited[G[top][i]]= true;
  46.                 sack.push(G[top][i]);
  47.                 parent[G[top][i]]= top;
  48.  
  49.                 if(G[top][i]==mp[dst]) {
  50. //                    cout<<"Node Level of destination is: "<<nodeLevel[G[top][i]]<<"("<< id[G[top][i]] <<")"<<endl;
  51.                     return nodeLevel[G[top][i]];
  52.                 }
  53.             }
  54.         }
  55.     }
  56.     return -1;
  57. }
  58.  
  59. int main() {
  60.  
  61.     //freopen("input.txt", "r", stdin);
  62.  
  63.     int n;
  64.     bool flag= false;
  65.     while(cin>>n) {
  66.         cr=0;
  67.         string id[(2*n)+3];
  68.         int parent[(2*n)+3];
  69.         memset(parent, 0, sizeof parent);
  70.         bool visited[(2*n)+3];
  71.         memset(visited, false, sizeof visited);
  72.  
  73.         string x, y;
  74.         map<string,int> mp;
  75.         map<int,string> mpp;
  76.  
  77.         for(int i=0; i<n; i++) {
  78.             cin>>x>>y;
  79.  
  80.             if(mp[x]==0 || mp[x]== NULL) {
  81.                 ++cr;
  82.                 mp[x]= cr;
  83.                 mpp[cr]= x;
  84.                 id[mp[x]]= x;
  85.             }
  86.             if(mp[y]==0 || mp[y]==NULL) {
  87.                 ++cr;
  88.                 mp[y]= cr;
  89.                 mpp[cr]= y;
  90.                 id[mp[y]]= y;
  91.  
  92.             }
  93.  
  94.             G[mp[x]].push_back(mp[y]);
  95.             G[mp[y]].push_back(mp[x]);
  96.         }
  97.  
  98.         string src, dst;
  99.         cin>>src>>dst;
  100.  
  101. //        cout<<"Entering bfs"<<endl;
  102.         int reko= bfs(src, dst, mp, id, visited, parent);
  103. //        cout<<"Didn't make to the bfs"<<endl;
  104. //    cout<<"VALUE OF REKO IS: "<<reko<<endl;
  105.  
  106.         if(flag) cout<<endl;
  107.         flag= true;
  108.  
  109.         if(reko>0) {
  110.             string newSrc= dst;
  111.             string node;
  112.             string path[reko+2];
  113.             int cnt=0;
  114.             int pre=reko;
  115.             int indexer=reko;
  116.             while(reko--) {
  117.                 path[indexer]= newSrc;
  118.  
  119.                 node.clear();
  120.                 node= id[parent[mp[newSrc]]];
  121.                 newSrc.clear();
  122.                 newSrc= node;
  123.                 ++cnt;
  124.                 --indexer;
  125.             }
  126.  
  127.             path[0]= src;
  128.  
  129.             for(int i=0; i<cnt; i++) {
  130.                 cout<<path[i]<<" "<<path[i+1]<<endl;
  131.             }
  132.  
  133.         } else if(reko==-1) cout<<"No route"<<endl;
  134.         clean(cr);
  135.     }
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement