Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. bool color[32];
  6.  
  7. int cost[32];
  8.  
  9. vector<int>graph[32];
  10.  
  11. map<string,int>mp;
  12.  
  13. string source,dest;
  14.  
  15. void bfs()
  16. {
  17.     int i,j;
  18.  
  19.     queue<int>Q;
  20.  
  21.     Q.push(mp[source]);
  22.  
  23.     color[mp[source]] = true;
  24.  
  25.     cost[mp[source]] = 0;
  26.  
  27.     while(!Q.empty())
  28.     {
  29.         i = Q.front();
  30.  
  31.         Q.pop();
  32.  
  33.         int sz = graph[i].size();
  34.  
  35.         for(j=0; j<sz; j++)
  36.         {
  37.             int u = graph[i][j];
  38.  
  39.             if(!color[u])
  40.             {
  41.                 color[u] = true;
  42.  
  43.                 cost[u] = cost[i]+1;
  44.  
  45.                 Q.push(u);
  46.             }
  47.         }
  48.     }
  49. }
  50.  
  51. int main()
  52. {
  53.     ios_base::sync_with_stdio(0);
  54.  
  55.     cin.tie(0);
  56.  
  57.     int test,x,warehouse,legs,query,i,key,request;
  58.  
  59.     string input,s,d;
  60.  
  61.     cin>>test;
  62.  
  63.     cout<<"SHIPPING ROUTES OUTPUT\n\n";
  64.  
  65.     for(x=1; x<=test; x++)
  66.     {
  67.         cout<<"DATA SET  "<<x<<"\n\n";
  68.  
  69.         cin>>warehouse>>legs>>query;
  70.  
  71.         for(i=1; i<=warehouse; i++)
  72.         {
  73.             cin>>input;
  74.         }
  75.  
  76.         key = 0;
  77.  
  78.         for(i=1; i<=legs; i++)
  79.         {
  80.             cin>>s>>d;
  81.  
  82.             if(mp.find(s)==mp.end())
  83.             {
  84.                 mp[s] = ++key;
  85.             }
  86.  
  87.             if(mp.find(d)==mp.end())
  88.             {
  89.                 mp[d] = ++key;
  90.             }
  91.  
  92.             graph[mp[s]].push_back(mp[d]);
  93.  
  94.             graph[mp[d]].push_back(mp[s]);
  95.         }
  96.  
  97.         for(i=1; i<=query; i++)
  98.         {
  99.             cin>>request>>source>>dest;
  100.  
  101.             bfs();
  102.  
  103.             if(cost[mp[dest]]!=0)
  104.             {
  105.                 cout<<"$"<<request*cost[mp[dest]]*100<<"\n";
  106.             }
  107.             else
  108.             {
  109.                 cout<<"NO SHIPMENT POSSIBLE"<<"\n";
  110.             }
  111.  
  112.             memset(cost,0,sizeof(cost));
  113.  
  114.             memset(color,false,sizeof(color));
  115.         }
  116.  
  117.         cout<<"\n";
  118.  
  119.         mp.clear();
  120.  
  121.         for(i=0; i<=warehouse; i++)
  122.         {
  123.             graph[i].clear();
  124.         }
  125.     }
  126.  
  127.     cout<<"END OF OUTPUT\n";
  128.  
  129.     return 0;
  130. }