-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1671 - Shortest Routes I.cpp
More file actions
50 lines (42 loc) · 967 Bytes
/
1671 - Shortest Routes I.cpp
File metadata and controls
50 lines (42 loc) · 967 Bytes
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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll, int> edge;
void solve() {
int n, m;
cin >> n >> m;
vector<ll> d(n, LONG_LONG_MAX);
vector<map<int, ll>> e(n);
for (int i = 0; i < m; i++) {
int u, v;
ll w;
cin >> u >> v >> w;
if (e[u - 1].count(v - 1))
e[u - 1][v - 1] = min(w, e[u - 1][v - 1]);
else
e[u - 1][v - 1] = w;
}
priority_queue<edge, vector<edge>, greater<edge>> q;
q.push({0, 0});
d[0] = 0;
while (!q.empty()) {
auto [newd, u] = q.top();
q.pop();
if (newd > d[u]) continue;
d[u] = newd;
for (auto [v, w] : e[u])
if (d[v] > d[u] + w) {
d[v] = d[u] + w;
q.push({d[v], v});
}
}
for (int i = 0; i < n; i++) cout << d[i] << " ";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) solve();
}