Advertisement
Guest User

Uri 1148.cpp

a guest
Jul 16th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define MAX 500
  4. #define INFINITO 1000000
  5.  
  6. using namespace std;
  7.  
  8. int dijkstra(int m[][MAX], int from, int to, int n)
  9. {
  10.     priority_queue<pair<int, int>, vector<pair<int,int>>, greater<pair<int, int>>> pq;
  11.     vector<bool> visiteds (n, false);
  12.  
  13.     pq.push(make_pair(0, from));
  14.     visiteds[from] = true;
  15.  
  16.     while (not pq.empty())
  17.     {
  18.         auto actual = pq.top();
  19.         pq.pop();
  20.  
  21.         if (actual.second == to)
  22.             return actual.first;
  23.  
  24.         for (int i = 0;i < n; ++i)
  25.         {
  26.             if (m[actual.second][i] != -1)
  27.             {
  28.                 if (not visiteds[i])
  29.                 {
  30.                     visiteds[i] = true;
  31.                     int cost = m[actual.second][i] + actual.first;
  32.                     pq.push(make_pair(cost, i));
  33.                 }
  34.             }
  35.         }
  36.     }
  37.  
  38.     return -1;
  39. }
  40.  
  41. int dijkstra2(int m[][MAX], int from, int to, int n)
  42. {
  43.     priority_queue<pair<int, int>, vector<pair<int,int>>, greater<pair<int, int>>> pq;
  44.     vector<bool> visiteds (n, false);
  45.     vector<int> dist (n, INFINITO);
  46.  
  47.     dist[from] = 0;
  48.     pq.push(make_pair(dist[from], from));
  49.  
  50.     while (!pq.empty())
  51.     {
  52.         auto actual = pq.top();
  53.         pq.pop();
  54.  
  55.         if (not visiteds[actual.second])
  56.         {
  57.             visiteds[actual.second] = true;
  58.  
  59.             for (int i = 0;i < n; ++i)
  60.             {
  61.                 if (m[actual.second][i] != -1)
  62.                 {
  63.                     if (dist[i] > actual.first + m[actual.second][i])
  64.                     {
  65.                         dist[i] = actual.first + m[actual.second][i];
  66.                         pq.push(make_pair(actual.first + m[actual.second][i], i));
  67.                     }
  68.                 }
  69.             }
  70.         }
  71.     }
  72.  
  73.     return (dist[to] == INFINITO ? -1 : dist[to]);
  74. }
  75.  
  76. int main()
  77. {
  78.     int N, E;
  79.  
  80.     while (cin >> N >> E, N || E)
  81.     {
  82.         int m[MAX][MAX];
  83.         memset(m, -1, sizeof(m));
  84.  
  85.         for (int i = 0;i < E; ++i)
  86.         {
  87.             int from, to, w;
  88.             cin >> from >> to >> w;
  89.             m[from - 1][to - 1] = w;
  90.  
  91.             if (m[to - 1][from - 1] != -1)
  92.             {
  93.                 m[from - 1][to - 1] = m[to - 1][from - 1] = 0;
  94.             }
  95.         }
  96.  
  97.         int k;
  98.         cin >> k;
  99.         while (k--)
  100.         {
  101.             int from, to;
  102.             cin >> from >> to;
  103.             int result = dijkstra2(m, from - 1, to - 1, N);
  104.  
  105.             if (result != -1)
  106.                 cout << result << endl;
  107.             else
  108.                 cout << "Nao e possivel entregar a carta" << endl;
  109.         }
  110.         cout << endl;
  111.     }
  112.  
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement