-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dij.cpp
49 lines (49 loc) · 953 Bytes
/
Dij.cpp
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<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
using namespace std;
const int N=100010,M=N*2;
int n,m,s,x,y,z;
int dis[N];
bool vis[N];
int head[N],ver[M],edge[M],Next[M],tot;
void add(int x,int y,int z){
ver[++tot]=y,edge[tot]=z,Next[tot]=head[x],head[x]=tot;
}
struct node{
int dis,pos;
};
bool operator < (const node &x,const node &y){
return x.dis>y.dis;
}
priority_queue<node> q;
void dijkstra(){
dis[s]=0;
q.push((node){0,s});
while(!q.empty()){
node tmp=q.top();q.pop();
int x=tmp.pos;
if(vis[x]) continue;
vis[x]=1;
for(int i=head[x];i;i=Next[i]){
int y=ver[i];
if(dis[y]>dis[x]+edge[i]){
dis[y]=dis[x]+edge[i];
if(!vis[y]) q.push((node){dis[y],y});
}
}
}
}
int main(){
scanf("%d%d%d",&n,&m,&s);
memset(dis,0x3f,sizeof dis);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
}
dijkstra();
for(int i=1;i<=n;i++) printf("%d ",dis[i]);
return 0;
}