Advertisement
Josif_tepe

Untitled

Feb 2nd, 2025
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. using namespace std;
  5. const int maxn = 105;
  6. vector<pair<int, int>> graph[maxn];
  7. int dist[maxn][10][500];
  8. struct node {
  9.     int idx, shortest_distance, k_left, path_with_automation;
  10.     node () {}
  11.     node(int _idx, int _shortest_distance, int _k_left, int _path_with_automation) {
  12.         idx = _idx;
  13.         shortest_distance = _shortest_distance;
  14.         k_left = _k_left;
  15.         path_with_automation = _path_with_automation;
  16.     }
  17.    
  18.     bool operator < (const node & tmp) const {
  19.         return shortest_distance > tmp.shortest_distance;
  20.     }
  21.    
  22. };
  23. int main() {
  24.     int n, x;
  25.     cin >> n >> x;
  26.     int K, L;
  27.     cin >> K >> L;
  28.    
  29.     int m;
  30.     cin >> m;
  31.    
  32.     for(int i = 0; i < m; i++) {
  33.         int a, b, c;
  34.         cin >> a >> b >> c;
  35.         graph[a].push_back(make_pair(b, c));
  36.         graph[b].push_back(make_pair(a, c));
  37.     }
  38.    
  39.     for(int i = 0; i <= n; i++) {
  40.         for(int j = 0; j <= K; j++) {
  41.             for(int k = 0; k <= L; k++) {
  42.                 dist[i][j][k] = 2e9;
  43.             }
  44.         }
  45.     }
  46.    
  47.     priority_queue<node> pq;
  48.     pq.push(node(1, 0, K, L));
  49.     dist[1][0][0] = 0;
  50.    
  51.     while(!pq.empty()) {
  52.         node c = pq.top();
  53.         pq.pop();
  54.        
  55.         int new_path = c.path_with_automation;
  56.         if(c.idx <= x and new_path > 0) {
  57.             new_path = 0;
  58.         }
  59.         if(new_path > 0) {
  60.             for(int i = 0; i < (int) graph[c.idx].size(); i++) {
  61.                 int neighbour = graph[c.idx][i].first;
  62.                 int weight = graph[c.idx][i].second;
  63.                 if(new_path - weight >= 0 and c.shortest_distance < dist[neighbour][c.k_left][new_path - weight]) {
  64.                     dist[neighbour][c.k_left][new_path - weight] = c.shortest_distance;
  65.                     pq.push(node(neighbour, c.shortest_distance, c.k_left, new_path - weight));
  66.                 }
  67.             }
  68.             new_path = 0;
  69.         }
  70.        
  71.         for(int i = 0; i < (int) graph[c.idx].size(); i++) {
  72.             int neighbour = graph[c.idx][i].first;
  73.             int weight = graph[c.idx][i].second;
  74.             if(c.shortest_distance + weight < dist[neighbour][c.k_left][new_path]) {
  75.                 dist[neighbour][c.k_left][new_path] = c.shortest_distance + weight;
  76.                 pq.push(node(neighbour, c.shortest_distance + weight, c.k_left, new_path));
  77.             }
  78.         }
  79.        
  80.         if(c.k_left > 0) {
  81.             new_path = L;
  82.             for(int i = 0; i < (int) graph[c.idx].size(); i++) {
  83.                 int neighbour = graph[c.idx][i].first;
  84.                 int weight = graph[c.idx][i].second;
  85.                
  86.                 if(new_path - weight >= 0 and c.shortest_distance < dist[neighbour][c.k_left - 1][new_path - weight]) {
  87.                     dist[neighbour][c.k_left - 1][new_path - weight] = c.shortest_distance;
  88.                     pq.push(node(neighbour, c.shortest_distance, c.k_left - 1, new_path - weight));
  89.                 }
  90.             }
  91.         }
  92.        
  93.     }
  94.     int result = 2e9;
  95.    
  96.     for(int i = 0; i <= K; i++) {
  97.         for(int j = 0; j <= L; j++) {
  98.             result = min(result, dist[n][i][j]);
  99.         }
  100.     }
  101.     cout << result << endl;
  102.    
  103.     return 0;
  104. }
  105.  
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement