-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDijkstra.cpp
More file actions
33 lines (29 loc) · 590 Bytes
/
Dijkstra.cpp
File metadata and controls
33 lines (29 loc) · 590 Bytes
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
const int MOD = 1e9+7;
const int MAX = 1e7;
vector<int> dis;
vector<bool> processed(MAX, false);
const int inf = INFINITY;
void dijkstra(int source){
priority_queue<pair<int,int>> q;
q.push({0,source});
dis.resize(MAX,inf);
dis[source] = 0;
while(!q.empty()){
int u = q.top().second;
q.pop();
if(processed[u]) continue;
processed[u] = true;
for(auto y : G[u]){
int v = y.first, w = y.second;
if(dis[u] + w < dis[v]){
dis[v] = dis[u] + w;
q.push(-dis[v],v);
}
}
}
}
int main()
{
dijkstra(source);
return 0;
}