#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> #include<queue> const int MaxN=100010; int last[MaxN],dis[MaxN],cnt; bool vis[MaxN]; int n,m,s; struct edge { int dis,to,next; }e[MaxN*5]; struct node { int dis; int pos; bool operator < (const node &x)const { return x.dis < dis; } }; std::priority_queue<node> q; void add(int u,int v,int d) { cnt++; e[cnt].dis=d; e[cnt].to=v; e[cnt].next=last[u]; last[u]=cnt; } void dijkstra() { dis[s]=0; q.push((node){0,s}); while(!q.empty()) { node tmp=q.top(); q.pop(); int x=tmp.pos,d=tmp.dis; if(vis[x]) continue; vis[x]=1; for(int i=last[x];i;i=e[i].next) { int y=e[i].to; if(dis[y]>dis[x]+e[i].dis) { dis[y]=dis[x]+e[i].dis; if(!vis[y]) { q.push((node){dis[y], y}); } } } } } int main() { scanf("%d%d%d", &n, &m, &s); for(int i=1;i<=n;++i)dis[i]=0x7fffffff; for(int i=0;i<m;++i) { int u,v,d; scanf("%d%d%d",&u,&v,&d); add(u,v,d); } dijkstra(); for(int i=1;i<=n;i++) printf("%d ",dis[i]); return 0; }