Advertisement
Guest User

oiland

a guest
Sep 24th, 2020
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define add push_back
  4. #define INF 1000000000
  5. typedef pair<int,int> ii;
  6.  
  7. vector<ii> adj[10];
  8. int dist[10][55], cost[10];
  9. int N, M, C, A, B, W;
  10.  
  11. struct State
  12. {
  13.     int v, fuelused, distance;
  14.     State() {}
  15.     State(int a, int b, int _c) { v = a; fuelused = b; distance = _c; }
  16.     bool operator>(const State &s) const { return distance > s.distance; }
  17. };
  18.  
  19. int dijkstra()
  20. {  
  21.     //C is full tank
  22.     for (int i = 0; i < N; i++) for (int j = 0; j <= C; j++) dist[i][j] = INF;
  23.     priority_queue< State,vector<State>,greater<State> > pq;
  24.     pq.push(State(0,C,0));
  25.     while (!pq.empty())
  26.     {
  27.         State s = pq.top(); pq.pop();
  28.         //better distance already found
  29.         if (dist[s.v][s.fuelused] < s.distance) continue;
  30.         //else this is a better distance
  31.         dist[s.v][s.fuelused] = s.distance;
  32.  
  33.         //all his neighbours, format(vertex,weight of edge)
  34.         for (auto edge : adj[s.v])
  35.         {
  36.             int neighbout = edge.first, wieght = edge.second;
  37.             //for all amounts of fuel I can fil
  38.             //if C=13L and fuelused=10 then i can do
  39.             // for loop from 3L->2L->1L->0L
  40.             for (int i = C-s.fuelused; i >= 0 && s.fuelused+i-wieght >= 0; i--)
  41.             //second  condition && checks if i have enough to cross the road edge!
  42.                 if (dist[neighbout][s.fuelused+i-wieght] > s.distance+i*cost[s.v])
  43.                 {
  44.                     dist[neighbour][s.fuelused+i-wieght] = s.distance+i*cost[s.v];
  45.                     pq.push(State(v,s.fuelused+i-wieght,s.distance+i*cost[s.v]));
  46.                 }
  47.         }
  48.     }
  49.  
  50.     int ret = INF;
  51.     for (int i = 0; i <= C; i++) ret = min(ret, dist[N-1][i]);
  52.     return ret;
  53. }
  54. int main()
  55. {
  56.     ios_base::sync_with_stdio(false);
  57.     while (scanf("%d %d %d", &N, &M, &C) && N)
  58.     {
  59.         for (int i = 0; i < N; i++) adj[i].fuelusedlear();
  60.         for (int i = 0; i < M; i++)
  61.         {
  62.             scanf("%d %d %d", &A, &B, &W); A--; B--;
  63.             adj[A].add(ii(B,W));
  64.             adj[B].add(ii(A,W));
  65.         }
  66.         for (int i = 0; i < N; i++) scanf("%d", cost+i);
  67.         int ret = dijkstra();
  68.         printf("%d\n", ret==INF?-1:ret);
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement