Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define MAX 1001
- #define INF 1e9
- #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
- using namespace std;
- int v, e, k, dist[MAX] = {}, goal;
- vector<pair<int, int>> arr[MAX];
- int main() {
- fastio;
- cin >> e >> v;
- for (int a, b, c, i = 0; i < v; i++) {
- cin >> a >> b >> c;
- arr[a].push_back({ b,c });
- }
- for (int i = 0; i <= e; i++) dist[i] = INF;
- priority_queue<pair<int, int>> q;
- cin >> k >> goal;
- q.push({ 0,k });
- dist[k] = 0;
- while (!q.empty()) {
- int cost = -q.top().first, idx = q.top().second;
- q.pop();
- if (dist[idx] < cost) continue;
- for (int i = 0; i < arr[idx].size(); i++) {
- int next = arr[idx][i].first, next_cost = arr[idx][i].second;
- if (dist[next] > dist[idx] + next_cost) {
- dist[next] = dist[idx] + next_cost;
- q.push({ -dist[next],next });
- }
- }
- }
- cout << dist[goal];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement