Naxocist

LOGISTICS

May 2nd, 2022
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using ll = long long;
  5. using pi = pair<int, int>;
  6. using tiiii = tuple<int, int, int, int>;
  7.  
  8. const int N = 105;
  9. vector<pi> adj[N];
  10. int vis[N][N][2];
  11. int pr[N];
  12.  
  13.  
  14. int main() {
  15.     // freopen("input.in", "r", stdin);
  16.    
  17.     int n; scanf("%d", &n);
  18.     for(int i=1; i<=n; ++i) scanf("%d", &pr[i]);
  19.  
  20.     int s, e, lim; scanf("%d%d%d", &s, &e, &lim);
  21.  
  22.     int m; scanf("%d", &m);
  23.  
  24.     for(int i=0; i<m; ++i){
  25.         int u, v, w; scanf("%d%d%d", &u, &v, &w);
  26.         adj[u].push_back({v, w});
  27.         adj[v].push_back({u, w});
  28.     }
  29.  
  30.     priority_queue<tiiii, vector<tiiii>, greater<tiiii>> pq;
  31.     pq.emplace(0, 0, s, 1);
  32.  
  33.     while(pq.size()){
  34.         int cost, fuel, u, t;
  35.         tie(cost, fuel, u, t) = pq.top(); pq.pop();
  36.        
  37.         if(u == e && fuel == lim){
  38.             printf("%d", cost);
  39.             return 0;
  40.         }
  41.  
  42.         if(vis[u][fuel][t]) continue;
  43.         vis[u][fuel][t] = 1;
  44.  
  45.         if(t) pq.emplace(cost, lim, u, 0); // fill free fuel
  46.        
  47.  
  48.         if(fuel < lim) pq.emplace(cost + pr[u], fuel + 1, u, t); // fill fuel one unit
  49.        
  50.         for(pi x : adj[u]){
  51.             if(fuel < x.second) continue;
  52.             pq.emplace(cost, fuel-x.second, x.first, t);
  53.         }
  54.     }  
  55.  
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment