Advertisement
he_obviously

Untitled

Jul 3rd, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <queue>
  5. #include <set>
  6.  
  7. using namespace std;
  8.  
  9. long long INF = LLONG_MAX / 3;
  10.  
  11. int n, m;
  12. vector<vector<pair<int, long long>>> v;
  13. vector<long long> d;
  14. vector<bool> used;
  15.  
  16. int main() {
  17.    
  18.     ios_base::sync_with_stdio(0);
  19.     cin.tie(0); cout.tie(0);
  20.  
  21.     cin >> n >> m;
  22.  
  23.     v.resize(n + 1);
  24.     d.resize(n + 1);
  25.     used.resize(n + 1);
  26.  
  27.     for (int _ = 0; _ < m; ++_) {
  28.         int a, b; long long c;
  29.         cin >> a >> b >> c;
  30.         if (a == b) continue;
  31.         else v[a].push_back({ b, c });
  32.     }
  33.  
  34.     int t; cin >> t;
  35.  
  36.     for (int _ = 0; _ < t; ++_) {
  37.         int s, f;
  38.         cin >> s >> f;
  39.         long long k = 0;
  40.         fill(d.begin(), d.end(), INF);
  41.         fill(used.begin(), used.end(), false);
  42.         d[s] = 0;
  43.         queue<int> q1 = {}, q2 = {}, q3 = {};
  44.         q1.push(s);
  45.         int cnt = 0;
  46.         for (int step = 0; step < n; ++step) {
  47.             while (!q1.empty()) {
  48.                 int start = q1.front();
  49.                 q1.pop();
  50.                 if (!used[start]) {
  51.                     used[start] = true;
  52.                     for (auto p : v[start]) {
  53.                         ++cnt;
  54.                         if (!used[p.first] && d[start] + p.second < d[p.first]) {
  55.                             d[p.first] = d[start] + p.second;
  56.                             if (d[p.first] >= k + 2000) {
  57.                                 q3.push(p.first);
  58.                             }
  59.                             else {
  60.                                 q2.push(p.first);
  61.                             }
  62.                         }
  63.                     }
  64.                 }
  65.             }
  66.             k += 1000;
  67.             q1 = q2;
  68.             q2 = q3;
  69.             q3 = {};
  70.         }
  71.         cout << cnt << "\n";
  72.         cout << ((d[f] == INF) ? -1 : d[f]) << "\n";
  73.     }
  74.  
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement