Advertisement
Josif_tepe

Untitled

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