-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstra.cpp
75 lines (65 loc) · 1.53 KB
/
Dijkstra.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
70
71
72
73
74
75
/*
In the name of Allah
Code by jahanaraco
Single Source Shortest Path
Dijkstras algorithm for Weighted Graphs
O(E lg N)
*/
#include <iostream>
#include <set>
#include <vector>
using namespace std;
const int MAXN = 1000 * 100;
int n, m, source, dist[MAXN];
vector<pair<int, int> > adj[MAXN];
set<pair<int, int> > S; // Set of candidates vertices
int parent[MAXN]; // "parent[i] = v" means v is last vertex updated i
void print_path(int v);
int main(){
cin >> n >> m >> source;
for(int i = 0; i < m; i++){
int v, u, w;
cin >> v >> u >> w;
adj[v].push_back(make_pair(u, w));
adj[u].push_back(make_pair(v, w));
}
for(int i = 1; i <= n; i++)
dist[i] = -1; // -1 means there's no path yet
dist[source] = 0;
S.insert(make_pair(0, source));
while(!S.empty() /*S.size() > 0 */){
int v = S.begin() -> second;
// int v = (*S.begin()).second;
S.erase(S.begin());
for(int i = 0; i < adj[v].size(); i++){
int u = adj[v][i].first;
int w = adj[v][i].second;
if(dist[u] == -1 or dist[u] > dist[v] + w){
if(dist[u] != -1)
S.erase(make_pair(dist[u], u));
dist[u] = dist[v] + w;
S.insert(make_pair(dist[u], u));
parent[u] = v; // note this line
}
}
}
/* // print shortest path for every i
for(int i = 1; i <= n; i++){
if(dist[i] != -1){
print_path(i);
cout << endl;
}
else
cout << -1 << endl;
}
*/
for(int i = 1; i <= n; i++)
cout << dist[i] << ' ' ;
cout << endl;
return 0;
}
void print_path(int v){
if(parent[v] != 0)
print_path(parent[v]);
cout << v << ' ';
}