-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShortest Routes I.cpp
69 lines (51 loc) · 1.3 KB
/
Shortest Routes I.cpp
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ed "\n"
#define eb emplace_back
#define pl " "
#define size size()
#define mpr make_pair
#define vec vector<ll>
#define prllsep cout << ed << " ------- " << ed
#define forr(i, a, b) for (ll i = a; i < b; i++)
const ll N = 1e5 + 7;
const ll INF = LLONG_MAX;
vector<vector<pair<ll, ll>>> adj_list(N);
map<ll, ll> value;
map<ll, bool> vis;
void Dijkstra(ll n, ll s) {
for (ll i = 0; i <= n; i++)
value[i] = INF;
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
pq.push({0, s});
value[s] = 0;
while (!pq.empty()) {
ll curr = pq.top().second;
pq.pop();
if (vis[curr])
continue;
vis[curr] = true;
forr(i, 0, adj_list[curr].size) {
ll neighbour = adj_list[curr][i].first;
ll weight = adj_list[curr][i].second;
if (!vis[neighbour] && value[neighbour] > value[curr] + weight) {
value[neighbour] = value[curr] + weight;
pq.push({value[neighbour], neighbour});
}
}
}
}
int main() {
ll n, m, s = 1;
cin >> n >> m;
for (ll i = 1; i < m + 1; i++) {
ll u, v, w;
cin >> u >> v >> w;
adj_list[u].eb(mpr(v, abs(w)));
}
Dijkstra(n, s);
forr(i, 1, n + 1) { cout << value[i] << pl; }
cout << ed;
}