Advertisement
midnight_sun

Untitled

Nov 15th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define MAX 1001
  3. #define INF 1e9
  4. #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
  5. using namespace std;
  6. int v, e, k, dist[MAX] = {}, goal;
  7. vector<pair<int, int>> arr[MAX];
  8. int main() {
  9.     fastio;
  10.     cin >> e >> v;
  11.     for (int a, b, c, i = 0; i < v; i++) {
  12.         cin >> a >> b >> c;
  13.         arr[a].push_back({ b,c });
  14.     }
  15.     for (int i = 0; i <= e; i++) dist[i] = INF;
  16.     priority_queue<pair<int, int>> q;
  17.     cin >> k >> goal;
  18.     q.push({ 0,k });
  19.     dist[k] = 0;
  20.     while (!q.empty()) {
  21.         int cost = -q.top().first, idx = q.top().second;
  22.         q.pop();
  23.         if (dist[idx] < cost) continue;
  24.         for (int i = 0; i < arr[idx].size(); i++) {
  25.             int next = arr[idx][i].first, next_cost = arr[idx][i].second;
  26.             if (dist[next] > dist[idx] + next_cost) {
  27.                 dist[next] = dist[idx] + next_cost;
  28.                 q.push({ -dist[next],next });
  29.             }
  30.         }
  31.     }
  32.     cout << dist[goal];
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement