-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
49 lines (38 loc) · 1.05 KB
/
dijkstra.cpp
File metadata and controls
49 lines (38 loc) · 1.05 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
#include <bits/stdc++.h>
using namespace std;
const int inf = 1<<29;
const int maxn = 1e5+13;
vector<pair<int, int>>adj[maxn+11];
int dist[maxn+11] = {};
bool vis[maxn+13] = {};
int main(){
int n = 10;
adj[1].push_back(make_pair(7, 20));
adj[1].push_back(make_pair(2, 5));
adj[1].push_back(make_pair(3, 1));
adj[3].push_back(make_pair(2, 10));
adj[3].push_back(make_pair(10, 1));
adj[10].push_back(make_pair(7, 16));
for(int i = 1;i <= n;i++) dist[i] = inf;
dist[1] = 0;
priority_queue<pair<int, int>>pq;
pq.push({0, 1});
while(!pq.empty()){
int a = pq.top().second;
pq.pop();
if (vis[a]) continue;
vis[a] = true;
for(auto &pr:adj[a]){
int b = pr.first;
int w = pr.second;
if (dist[a]+w < dist[b]){
dist[b] = dist[a]+w;
pq.push(make_pair(-dist[b], b));
}
}
}
for(int i = 1;i <= n;i++){
cout << "dist from " << 1 << " to " << i << ": " << dist[i] << endl;
}
return 0;
}