-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra.cpp
More file actions
42 lines (35 loc) · 846 Bytes
/
Dijkstra.cpp
File metadata and controls
42 lines (35 loc) · 846 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
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int, int> p;
vector<p> g[20001];
int dist[20001];
int par[20001];
int n;
void dijkstra(int v) {
priority_queue<p> pq;
pq.push({0, v}); dist[v] = 0;
while(!pq.empty()) {
int now = pq.top().y;
int cost = -pq.top().x;
pq.pop();
if(cost > dist[now]) continue;
for(auto i: g[now]) {
int nxt = i.x;
int nxtCost = cost + i.y;
if (nxtCost < dist[nxt]) {
nxtCost = dist[nxt];
pq.push({-nxtCost, nxt});
par[nxt] = now;
}
}
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
for(int i=1; i<=n; i++) dist[i] = 1e9;
for(int i=1; i<=n; i++) par[i] = -1;
dijkstra(1);
}