Advertisement
Josif_tepe

Untitled

Apr 28th, 2025
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <cstring>
  5. using namespace std;
  6. struct node {
  7.     int idx, shortest_time, money;
  8.    
  9.     node () {}
  10.     node(int _idx, int _shortest_time, int _money) {
  11.         idx = _idx;
  12.         shortest_time = _shortest_time;
  13.         money = _money;
  14.     }
  15.    
  16.     bool operator < (const node & tmp) const {
  17.         if(shortest_time == tmp.shortest_time) {
  18.             return money > tmp.money;
  19.         }
  20.         return shortest_time > tmp.shortest_time;
  21.     }
  22.    
  23. };
  24.  
  25. struct vertex {
  26.     int b, time, money;
  27.     vertex() {}
  28.     vertex(int _b, int _time, int _money) {
  29.         b = _b;
  30.         time = _time;
  31.         money = _money;
  32.     }
  33.    
  34. };
  35.  
  36.  
  37. vector<vertex> graph[3005];
  38. bool visited[3005][2005];
  39. int main() {
  40.     memset(visited, false, sizeof visited);
  41.    
  42.     int S, n, m;
  43.     cin >> S >> n >> m;
  44.    
  45.     for(int i = 0; i < m; i++) {
  46.         int a, b, c, d;
  47.         cin >> a >> b >> c >> d;
  48.  
  49.         graph[a].push_back(vertex(b, c, d));
  50.         graph[b].push_back(vertex(a, c, d));
  51.     }
  52.    
  53.     priority_queue<node> pq;
  54.     pq.push(node(1, 0, 0));
  55.    
  56.     while(!pq.empty()) {
  57.         node c = pq.top();
  58.         pq.pop();
  59.        
  60.         if(c.idx == n) {
  61.             cout << c.shortest_time << endl;
  62.             return 0;
  63.         }
  64.        
  65.         if(visited[c.idx][c.money]) continue;
  66.         visited[c.idx][c.money] = true;
  67.        
  68.         for(int i = 0; i < (int) graph[c.idx].size(); i++) {
  69.             int neighbour = graph[c.idx][i].b;
  70.             int time = graph[c.idx][i].time;
  71.             int money = graph[c.idx][i].money;
  72.            
  73.             if(c.money + money <= S and !visited[neighbour][c.money + money]) {
  74.                 pq.push(node(neighbour, c.shortest_time + time, c.money + money));
  75.             }
  76.         }
  77.     }
  78.    
  79.     cout << -1 << endl;
  80.     return 0;
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement