-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD.cpp
More file actions
52 lines (44 loc) · 1.53 KB
/
Copy pathD.cpp
File metadata and controls
52 lines (44 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<vector<pair<int, long long>>> g(n);
for (int i = 0; i < m; ++i) {
int v, u;
long long w;
cin >> v >> u >> w;
w *= 2;
g[--v].push_back({--u, w});
g[u].push_back({v, w});
}
vector<long long> dist(n);
for (int i = 0; i < n; ++i) {
cin >> dist[i];
}
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
for (int i = 0; i < n; ++i) {
pq.push({dist[i], i});
}
while (!pq.empty()) {
int v = pq.top().second;
long long d = pq.top().first;
pq.pop();
if (d != dist[v]) {
continue;
}
for (auto &e : g[v]) {
int u = e.first;
long long w = e.second;
if (dist[u] > dist[v] + w) {
dist[u] = dist[v] + w;
pq.push({dist[u], u});
}
}
}
for (int i = 0; i < n; ++i) {
cout << dist[i] << ' ';
}
}