-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
46 lines (42 loc) · 903 Bytes
/
Copy pathdijkstra.cpp
File metadata and controls
46 lines (42 loc) · 903 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
43
44
45
46
// [dijkstra.cpp]
// 求從a點到其他點的最短距離
#include<bits/stdc++.h>
using namespace std;
#define INT long long int
#define PII pair<INT,INT>
#define hmax(a,b) ((a>b)?a:b)
#define hmin(a,b) ((a<b)?a:b)
const INT mxINT=1e18;
const INT mxn=1e5;
vector<PII> tree[mxn+5];//{node,weight}
INT n,m;
//n=number of nodes
//m=number of edges
INT dist[mxn+5];//distance from start
bool vis[mxn+5];//visited
void init(){
for(INT i=0;i<=n+1;i++){
tree[i].clear();
dist[i]=mxINT;
vis[i]=0;
}
}
void dijkstra(INT st){
priority_queue<PII,vector<PII>,greater<PII>> pq;
pq.push({0,st});
for(INT i=0;i<=n;i++)dist[i]=mxINT,vis[i]=0;
dist[st]=0;
while(!pq.empty()){
INT nw=pq.top().second;
pq.pop();
if(vis[nw])continue;
for(PII i:tree[nw]){
INT nxt=i.first,v=i.second;
if(dist[nw]+v<dist[nxt]){
dist[nxt]=dist[nw]+v;
pq.push({dist[nxt],nxt});
}
}
vis[nw]=1;
}
}