Advertisement
SuitNdtie

LOGISTICS

May 29th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<vector>
  3. #include<queue>
  4. using namespace std;
  5.  
  6. typedef long long int ll;
  7.  
  8. struct edge{
  9.     int v;
  10.     ll w;
  11. };
  12.  
  13. struct elem{
  14.     int vc;
  15.     int u;
  16.     ll fuel;
  17.     ll dis;
  18.     bool operator < (const elem& rhs)const
  19.     {
  20.         return dis > rhs.dis;
  21.     }
  22. };
  23.  
  24. int main()
  25. {
  26.     int n;
  27.     scanf("%d",&n);
  28.     ll cost[n+1];
  29.     for(int i = 1 ; i <= n ; i ++){
  30.         scanf("%lld",&cost[i]);
  31.     }
  32.     int b,e,c;
  33.     scanf("%d %d %d",&b,&e,&c);
  34.     int m;
  35.     scanf("%d",&m);
  36.    
  37.     vector<edge> adj[n+1];
  38.     for(int i = 0 ; i < m ; i ++){
  39.         int u,v; ll w;
  40.         scanf("%d %d %lld",&u,&v,&w);
  41.         adj[u].push_back({v,w});
  42.         adj[v].push_back({u,w});
  43.     }
  44.    
  45.     bool visited[2][n+1][c+1];for(int k = 0 ; k < 2 ; k ++)for(int i = 0 ; i <= n ; i ++)for(int j = 0 ; j <= c ; j ++)visited[k][i][j] = false;
  46.    
  47.     priority_queue<elem> pq;
  48.     pq.push({1,b,0,0});
  49.     while(!pq.empty()){
  50.         int u = pq.top().u;
  51.         ll fuel = pq.top().fuel;
  52.         ll dis = pq.top().dis;
  53.         int vc = pq.top().vc;
  54.         pq.pop();
  55.         if(visited[vc][u][fuel])continue;
  56.         visited[vc][u][fuel] = true;
  57.     //  printf("test (%d,%d,%lld) : %lld\n",vc,u,fuel,dis);
  58.         if(u == e && fuel == c){
  59.             printf("%lld",dis);
  60.             return 0;
  61.         }
  62.         if(vc == 1/* && fuel < c*/){
  63.             pq.push({0,u,c,dis});
  64.         }
  65.         if(fuel+1 <= c){
  66.             pq.push({vc,u,fuel+1,dis+cost[u]});
  67.         }
  68.        
  69.         for(int i = 0 ; i < adj[u].size() ; i ++){
  70.             int v = adj[u][i].v;
  71.             ll w = adj[u][i].w;
  72.            
  73.             if(fuel >= w){
  74.                 ll nfuel = fuel - w;
  75.                 if(!visited[vc][v][nfuel]){
  76.                     pq.push({vc,v,nfuel,dis});
  77.                 }
  78.                
  79.             }
  80.         }
  81.     }
  82.     printf("error -1\n");
  83.     return -1;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement