-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDijkstra
More file actions
40 lines (36 loc) · 738 Bytes
/
Dijkstra
File metadata and controls
40 lines (36 loc) · 738 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
/*
priority queue
struct Data{
int idx,v ;
friend bool operator <(const Data& a,const Data& b){
if(a.v==b.v)
return a.idx>b.idx ; /// priority queue returns opposite . ...
else
return a.v>b.v ;
}
}obj;
/**/
vector<array<int,3>>adj[N] ;
int dijkstra()
{
vector<bool>mark(n+1,0) ;
fill(d,d+n+2,oo) ;
d[1]=0 ;
priority_queue<pii>Q ;
Q.push({0,1}) ;
while(Q.size())
{
int v = Q.top().second ;
Q.pop() ;
if(mark[v])continue ;
mark[v]=1 ;
for(auto [u,w,t]:adj[v])
{
if(d[u]>w+d[v])
{
d[u] = w + d[v] ;
Q.push({-d[u],u}) ;
}
}
}
}