Advertisement
mickypinata

TOI14: Logistics

Nov 20th, 2020
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef tuple<int, int, int, int> tiiii;
  5. typedef pair<int, int> pii;
  6.  
  7. const int N = 100;
  8.  
  9. vector<pii> adj[N + 10];
  10. int fuelCost[N + 10];
  11. int nVertex, nEdge, mxFuel, st, ed;
  12.  
  13. int main(){
  14.  
  15.     scanf("%d", &nVertex);
  16.     for(int i = 1; i <= nVertex; ++i){
  17.         scanf("%d", &fuelCost[i]);
  18.     }
  19.     scanf("%d %d %d %d", &st, &ed, &mxFuel, &nEdge);
  20.     for(int i = 1; i <= nEdge; ++i){
  21.         int u, v, w;
  22.         scanf("%d %d %d", &u, &v, &w);
  23.         adj[u].emplace_back(v, w);
  24.         adj[v].emplace_back(u, w);
  25.     }
  26.  
  27.     priority_queue<tiiii, vector<tiiii>, greater<tiiii>> priQ; // {cost, u, fuel, free}
  28.     vector<vector<vector<int>>> cost(nVertex + 1, vector<vector<int>>(mxFuel + 1, vector<int>(2, 1e9)));
  29.     vector<vector<vector<bool>>> visited(nVertex + 1, vector<vector<bool>>(mxFuel + 1, vector<bool>(2, false)));
  30.     cost[st][0][1] = 0;
  31.     priQ.emplace(0, st, 0, 1);
  32.     while(!priQ.empty()){
  33.         int u = get<1>(priQ.top());
  34.         int fuel = get<2>(priQ.top());
  35.         int free = get<3>(priQ.top());
  36.         priQ.pop();
  37.         if(visited[u][fuel][free]){
  38.             continue;
  39.         }
  40.         visited[u][fuel][free] = true;
  41.         if(u == ed && fuel == mxFuel){
  42.             cout << cost[ed][mxFuel][free];
  43.             break;
  44.         }
  45.         for(int i = fuel + 1; i <= mxFuel; ++i){
  46.             if(free == 1 && !visited[u][i][0] && cost[u][fuel][1] < cost[u][i][0]){
  47.                 cost[u][i][0] = cost[u][fuel][1];
  48.                 priQ.emplace(cost[u][i][0], u, i, 0);
  49.             }
  50.             if(!visited[u][i][free] && cost[u][fuel][free] + (i - fuel) * fuelCost[u] < cost[u][i][free]){
  51.                 cost[u][i][free] = cost[u][fuel][free] + (i - fuel) * fuelCost[u];
  52.                 priQ.emplace(cost[u][i][free], u, i, free);
  53.             }
  54.         }
  55.         for(pii nxt : adj[u]){
  56.             int v = nxt.first;
  57.             int w = nxt.second;
  58.             if(fuel >= w && !visited[v][fuel - w][free] && cost[u][fuel][free] < cost[v][fuel - w][free]){
  59.                 cost[v][fuel - w][free] = cost[u][fuel][free];
  60.                 priQ.emplace(cost[v][fuel - w][free], v, fuel - w, free);
  61.             }
  62.         }
  63.     }
  64.  
  65.     return 0;
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement