MAGCARI

Untitled

Aug 30th, 2022 (edited)
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. /*
  2.     Task        :
  3.     Author      : Phumipat C. [MAGCARI]
  4.     Language    : C++
  5. */
  6. #include<bits/stdc++.h>
  7. using namespace std;
  8. struct A{
  9.     // v is current node
  10.     // p is current cost
  11.     // oil is current oil
  12.     // tic is current used ticket
  13.     int v,p,oil,tic;
  14.     bool operator < (const A&o) const{
  15.         if(p != o.p)    return p>o.p;
  16.         else            return oil<o.oil;
  17.     }
  18. };
  19. priority_queue<A > heap;
  20. struct B{
  21.     // v is edge's destination
  22.     // w is edge's weight
  23.     int v,w;
  24. };
  25. vector<B > g[110];
  26. int price[110],cost[110][110][2];
  27. //Third dimension is amout of used ticket
  28. int main(){
  29.     int n,st,en,cap,m,u,v,w;
  30.     scanf("%d",&n);
  31.     for(int i=1;i<=n;i++)
  32.         scanf("%d",&price[i]);
  33.     scanf("%d %d %d %d",&st,&en,&cap,&m);
  34.     while(m--){
  35.         scanf("%d %d %d",&u,&v,&w);
  36.         g[u].push_back({v,w});
  37.         g[v].push_back({u,w});
  38.     }
  39.     //set default value of array to infinity except for the first node
  40.     for(int i=1;i<=n;i++)
  41.         for(int j=0;j<=cap;j++)
  42.             cost[i][j][0] = cost[i][j][1] = 1e9;
  43.     //start Dijkstra process
  44.     cost[st][0][0] = 0;
  45.     heap.push({st,0,0,0});
  46.     while (!heap.empty()){
  47.         A now = heap.top();
  48.         heap.pop();
  49.         if(now.v == en && now.oil == cap){
  50.             printf("%d\n",now.p);
  51.             break;
  52.         }
  53.         // use ticket
  54.         if(now.tic == 0 && cost[now.v][cap][1] > now.p){
  55.             cost[now.v][cap][1] = now.p;
  56.             heap.push({now.v,now.p,cap,1});
  57.         }
  58.         //buy 1 unit of oil
  59.         if(now.oil < cap && cost[now.v][now.oil+1][now.tic] > now.p + price[now.v]){
  60.             cost[now.v][now.oil+1][now.tic] = now.p + price[now.v];
  61.             heap.push({now.v,now.p + price[now.v],now.oil+1,now.tic});
  62.         }
  63.         //travel to nearby node
  64.         for(auto x:g[now.v]){
  65.             if(x.w <= now.oil && cost[x.v][now.oil-x.w][now.tic] > now.p){
  66.                 cost[x.v][now.oil-x.w][now.tic] = now.p;
  67.                 heap.push({x.v,now.p,now.oil-x.w});
  68.             }
  69.         }
  70.     }
  71.     // printf("%d\n",min(cost[en][cap][0],cost[en][cap][1]));
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment