Advertisement
SuitNdtie

Trip Planner

May 28th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 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. ll min(ll a,ll b){
  9.     return (a < b ? a : b);
  10. }
  11.  
  12. struct edge{
  13.     int v;
  14.     ll w;
  15. };
  16.  
  17. struct elem{
  18.     int u;
  19.     ll
  20.      fuel;
  21.     ll dis;
  22.     bool operator < (const elem& rhs)const
  23.     {
  24.         return dis > rhs.dis;
  25.     }
  26. };
  27.  
  28. int const N = 1010;
  29.  
  30. vector<edge> adj[N];
  31.  
  32. int main()
  33. {
  34.     int n,m;
  35.     scanf("%d %d",&n,&m);
  36.     ll price[n];
  37.     for(int i = 0 ; i < n ; i ++){
  38.         scanf("%lld",&price[i]);
  39.     }
  40.     for(int i = 0 ; i < m ; i ++){
  41.         int u,v;
  42.         ll w;
  43.         scanf("%d %d %lld",&u,&v,&w);
  44.         adj[u].push_back({v,w});
  45.         adj[v].push_back({u,w});
  46.     }
  47.     ll dist[n][110];
  48.     bool visited[n][110];
  49.         for(int i = 0 ; i < n ; i ++)for(int j = 0 ; j < 110 ; j ++){
  50.             dist[i][j] = 1e18;
  51.             visited[i][j] = false; 
  52.         }
  53.    
  54.     int T;
  55.     scanf("%d",&T);
  56.     for(int t = 0 ; t < T ; t ++){
  57.         int c,b,e;
  58.         scanf("%d %d %d",&c,&b,&e);
  59.         for(int i = 0 ; i < n ; i ++){
  60.             for(int j = 0 ; j < 110 ; j ++){
  61.                 dist[i][j] = 1e18;
  62.                 visited[i][j] = false; 
  63.             }
  64.         }
  65.         priority_queue<elem> pq;
  66.         dist[b][0] = 0;
  67.         pq.push({b,0,dist[b][0]});
  68.         bool check = true;
  69.         while(!pq.empty()){
  70.             int u = pq.top().u;
  71.             ll fuel = pq.top().fuel;
  72.             ll dis = pq.top().dis;
  73.             pq.pop();
  74.             if(visited[u][fuel])continue;
  75.             visited[u][fuel] = true;
  76.             if(u == e){
  77.                 printf("%lld\n",dis);
  78.                 check = false;
  79.                 break; 
  80.             }
  81.             if(fuel+1 <= c){
  82.                 if(!visited[u][fuel+1] && dist[u][fuel] + price[u] < dist[u][fuel+1]){
  83.                     dist[u][fuel+1] = dist[u][fuel] + price[u];
  84.                     pq.push({u,fuel+1,dist[u][fuel+1]});   
  85.                 }
  86.             }
  87.             for(int i = 0 ; i < adj[u].size() ; i ++){
  88.                 int v = adj[u][i].v;
  89.                 ll w = adj[u][i].w;
  90.                 if(fuel <= c){
  91.                     if (fuel < w) continue;
  92.                     if(!visited[v][fuel-w] && dist[u][fuel] < dist[v][fuel-w]){
  93.                         dist[v][fuel-w] = dist[u][fuel];
  94.                         pq.push({v,fuel-w,dist[v][fuel-w]});
  95.                     }
  96.                 }
  97.             }
  98.         }
  99.         while(!pq.empty())pq.pop();
  100.         if(check){
  101.             printf("impossible\n");
  102.         }
  103.     }
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement