Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. const int N = 1e3 + 7;
  7. const int INF = 1e18;
  8.  
  9. vector<pair<pair<int,int>,int>> krawedz;
  10.  
  11. int odl[N];
  12. int pop[N];
  13.  
  14. int n, m, q, a, b, c;
  15.  
  16. void fb(int v)
  17. {
  18.     for(int i = 1; i <= n; i++)
  19.     {
  20.         odl[i] = INF;
  21.         pop[i] = 0;
  22.     }
  23.    
  24.     odl[v] = 0;
  25.    
  26.     for(int i = 1; i <= n; i++)
  27.     {
  28.         for(auto xd: krawedz)
  29.         {
  30.             if(odl[xd.first.first] != INF && odl[xd.first.second] > odl[xd.first.first] + xd.second)
  31.             {
  32.                 odl[xd.first.second] = odl[xd.first.first] + xd.second;
  33.                 pop[xd.first.second] = xd.first.first;
  34.             }
  35.         }
  36.     }
  37. }
  38.  
  39. int main()
  40. {
  41.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  42.    
  43.     cin >> n >> m >> q;
  44.    
  45.     for(int i = 1; i <= m; i++)
  46.     {
  47.         cin >> a >> b >> c;
  48.        
  49.         krawedz.push_back({{a,b},c});
  50.     }
  51.    
  52.     for(int i = 0; i < q; i++)
  53.     {
  54.         cin >> a >> b;
  55.        
  56.         fb(a);
  57.        
  58.         /*
  59.         cout << "odleglosc do b: " << odl[b] << ' ';
  60.        
  61.         cout << endl;
  62.         */
  63.        
  64.         if(odl[b] == INF)
  65.         {
  66.             cout << "NIE\n";
  67.         }
  68.        
  69.         else
  70.         {
  71.             cout << odl[b] << ' ';
  72.            
  73.             vector<int> wynik;
  74.            
  75.             wynik.push_back(b);
  76.            
  77.             int x = b;
  78.             while(pop[x]!=a)
  79.             {
  80.                 x = pop[x];
  81.                 wynik.push_back(x);
  82.             }
  83.            
  84.             wynik.push_back(a);
  85.            
  86.             cout << wynik.size() << ' ';
  87.            
  88.             while(!wynik.empty())
  89.             {
  90.                 cout<<wynik.back()<<' ';
  91.                 wynik.pop_back();
  92.             }
  93.         }
  94.         cout << '\n';
  95.     }
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement