forked from bamsarts/DS-ALGO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstra.cpp
44 lines (38 loc) · 898 Bytes
/
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
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 10000; // Max Node number
struct EDGE {
int u;
long long w;
bool operator < (const EDGE & o) const {
return w > o.w;
}
};
vector <EDGE> g[MAXN];
priority_queue <EDGE> heap;
long long dis[MAXN];
bool visited[MAXN];
int main(){
int e; cin >> e;
for (int i = 0; i < e; i++) {
// for each edge
int a, b, distance;
cin >> a >> b >> distance;
// from node a to node b with distance = distance
// note that edge is directed
g[a].push_back({b, distance});
}
int startNode;
cin >> startNode;
heap.push({startNode, 0});
while (!heap.empty()) {
auto t = heap.top(); heap.pop();
if (visited[t.u]) continue;
visited[t.u] = 1;
dis[t.u] = t.w;
for (auto x : g[t.u]) {
heap.push({x.u, t.w+x.w});
}
}
// distance from startNode to any node x is equal to dis[x]
}