#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 1000001
#define inf 1e9+7
int read()
{
    int f=1,x=0;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')f=-1; ch=getchar();}
    while (ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return f*x;
}
struct Edge{
    int v,w,next;
}e[N];
struct Node{
    int d,id;
    friend bool operator <(Node a,Node b)
    {
        return a.d>b.d;
    }
};
priority_queue<Node>q;
int inq[N],head[N],tot,d[N];
void add(int u,int v,int w)
{
    e[++tot].next=head[u];
    e[tot].v=v;
    e[tot].w=w;
    head[u]=tot;
}
void Dij(int s,int n)
{
    for(int i=1;i<=n;i++)
    d[i]=inf;
    d[s]=0;q.push((Node){0,s});
    while(!q.empty())
    {
        Node tmp=q.top();q.pop();
        int u=tmp.id;
        if(inq[u])continue;
        inq[u]=1;
        for(int i=head[u];i;i=e[i].next)
        {
            int v=e[i].v;
            if(d[v]>d[u]+e[i].w)
            {
                d[v]=d[u]+e[i].w;
                q.push((Node){d[v],v});
            }
        }
    }
}
int main()
{
    int n=read(),m=read(),s=read();
    for(int i=1;i<=m;i++)
    {
        int t1=read(),t2=read(),t3=read();
        add(t1,t2,t3);
    }
    Dij(s,n);
    for(int i=1;i<=n;i++)
    printf(d[i]==inf?"2147483647 ":"%d ",d[i]);
}
 
![]()