-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
53 lines (46 loc) · 850 Bytes
/
Copy pathdijkstra.cpp
File metadata and controls
53 lines (46 loc) · 850 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
47
48
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
const int N=150001,M=N*2;
int n,m,st,en;
int he[N],no[M],we[M],ne[M],idx;
int dist[N];
bool state[N];
void add(int a,int b,int c){
no[idx]=b;
we[idx]=c;
ne[idx]=he[a];
he[a]=idx++;
}
int dijk(){
memset(dist,0x3f,sizeof dist);
dist[st]=0;
priority_queue<pii,vector<pii>,greater<pii>>h;
h.push({0,st});
while(!h.empty()){
auto t=h.top();
h.pop();
state[t.second]=true;
for(int i=he[t.second];~i;i=ne[i]){
int j=no[i];
if(dist[j]>dist[t.second]+we[i]){
dist[j]=dist[t.second]+we[i];
h.push({dist[j],j});
}
}
}
return dist[en];
}
int main(){
scanf("%d%d%d%d",&n,&m,&st,&en);
memset(he,-1,sizeof he);
while(m--){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
int res=dijk();
printf("%d\n",res);
return 0;
}