forked from vaibhavpathak999/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstras_algorithm.cpp
More file actions
57 lines (50 loc) · 1.19 KB
/
Copy pathDijkstras_algorithm.cpp
File metadata and controls
57 lines (50 loc) · 1.19 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
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
// Node and weight
vector<pair<int, int>> v[100005];
int d[100005]; // distance
int main() {
int n, m; // vertices, edges
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y, w;
cin >> x >> y >> w;
v[x].push_back({y, w});
v[y].push_back({x, w});
}
// initially all nodes at INF distance
for (int i = 1; i <= n; ++i) d[i] = INF;
// source is 1.
d[1] = 0;
// distance 1st so it is sorted
set<pair<int, int>> s; //{distance, node}
s.insert({0, 1});
while (!s.empty()) {
int dis = s.begin()->first;
int vertex = s.begin()->second;
s.erase(s.begin());
for (pair<int, int> x : v[vertex]) {
int newDis = dis + x.second;
int newVer = x.first;
if (newDis < d[newVer]) {
s.erase({d[newVer], newVer});
d[newVer] = newDis; // update
s.insert({d[newVer], newVer});
}
}
}
for (int i = 1; i <= n; ++i) {
cout << d[i] << ' ';
}
return 0;
}
/*
5 5
1 4 100
1 3 200
2 1 100
5 2 200
3 5 300
*/
// 0 100 200 100 300