DuongNhi99

QBROBOT

Dec 2nd, 2020 (edited)
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4.  
  5. const int N = 505;
  6. const int oo = 5e6 + 5;
  7.  
  8. int n, m;
  9. int g[N], Time[N][N], Cost[N][N];
  10. int64_t d[N];
  11. vector<int> a[N];
  12.  
  13. void Dijkstra() {
  14.     priority_queue<pair<int, int64_t> > pq;
  15.  
  16.     fill(d + 1, d + n + 1, oo);
  17.     d[n] = 0;
  18.     pq.push({d[n], n});
  19.  
  20.     while(!pq.empty()) {
  21.         int u = pq.top().second;
  22.         int du = pq.top().first;
  23.         pq.pop();
  24.  
  25.         if(du > d[u]) continue;
  26.  
  27.         for(int v : a[u]) {
  28.             if(d[v] > d[u] + Time[u][v]) {
  29.                 d[v] = d[u] + Time[u][v];
  30.                 pq.push({d[v], v});
  31.             }
  32.         }
  33.     }
  34. }
  35.  
  36. struct edge {
  37.     int root, timer, costs;
  38. };
  39.  
  40. bool check(int mid) {
  41.     queue<edge> pq;
  42.     pq.push({1, 0, mid});
  43.  
  44.     while(!pq.empty()) {
  45.         int u = pq.front().root;
  46.         int tu = pq.front().timer;
  47.         int wu = pq.front().costs;
  48.         pq.pop();
  49.  
  50.         for(int v : a[u]) {
  51.             if(wu < Cost[u][v]) continue;
  52.             if(tu + Time[u][v] + d[v] > d[1]) continue;
  53.  
  54.             int wv = g[v] ? mid : wu - Cost[u][v];
  55.             int tv = tu + Time[u][v];
  56.  
  57.             if(v == n && tv == d[1])
  58.                 return true;
  59.  
  60.             pq.push({v, tv, wv});
  61.         }
  62.     }
  63.     return false;
  64. }
  65.  
  66. int main()
  67. {
  68.     //freopen("in.txt", "r", stdin);
  69.     //freopen("ROBOT.inp", "r", stdin);
  70.     //freopen("ROBOT.out", "w", stdout);
  71.     ios_base::sync_with_stdio(false);
  72.     cin.tie(NULL); cout.tie(NULL);
  73.  
  74.     cin >> n;
  75.     for(int i = 1; i <= n; i++)
  76.         cin >> g[i];
  77.     cin >> m;
  78.     for(int i = 1; i <= m; i++) {
  79.         int u, v, t, c;
  80.         cin >> u >> v >> t >> c;
  81.  
  82.         a[u].push_back(v);
  83.         a[v].push_back(u);
  84.         Time[u][v] = Time[v][u] = t;
  85.         Cost[u][v] = Cost[v][u] = c;
  86.     }
  87.  
  88.     Dijkstra();
  89.  
  90.     int64_t L = 1, R = oo, ans = 0;
  91.     while(L <= R) {
  92.         int mid = (L+R) / 2;
  93.         if(check(mid))
  94.             ans = mid, R = mid - 1;
  95.         else
  96.             L = mid + 1;
  97.     }
  98.  
  99.     cout << ans << '\n';
  100.  
  101.     return 0;
  102. }
  103.  
Add Comment
Please, Sign In to add comment