forked from gghati/Standard-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 14
/
dijkstra-shortest-path-dfs.cpp
55 lines (42 loc) · 1.07 KB
/
dijkstra-shortest-path-dfs.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
# include <bits/stdc++.h>
using namespace std;
# define fastt ios::sync_with_stdio(0); cin.tie(0);cout.tie(0)
# define testt int T; cin>>T; while(T--)
# define ll long long
# define ff first
# define ss second
// Dijkstra’s shortest path algorithm
// store the minimum distance from one node to the every other node.
# define NODE 100002
vector<pair<ll, ll>> arr[NODE];
ll d[NODE];
int main(){
fastt;
int n, m;
cin >> n >> m;
for (int i=0; i<m; i++) {
int a, b, c;
cin >> a >> b >> c;
arr[a].push_back({c, b});
}
priority_queue<pair<ll, ll>, std::vector<pair<ll, ll>>, greater<pair<ll, ll>> > pq;
pq.push({0, 1});
memset(d, 0x3f, sizeof(d));
d[1] = 0;
while (pq.size()) {
pair<ll, ll> p = pq.top(); // p.ff => dist, p.ss => index
pq.pop();
// leave the element which is already fixed.
if (d[p.ss] < p.ff)
continue;
for(auto nei: arr[p.ss]) {
if (d[nei.ss] > (nei.ff+d[p.ss]) ) {
d[nei.ss] = nei.ff + d[p.ss];
pq.push({ d[nei.ss], nei.ss });
}
}
}
for (int i=1; i<=n; i++)
cout << d[i] << " ";
cout << "\n";
}