import java.io.*;
import java.util.*;
/***
* 通过
* @author m
*
*/
public class P3371 {
static List<Edge> edges[];// 使用list数组存放边的信息
static boolean isVisited[];
static int result[] ;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
int N = nextInt(in);
int M = nextInt(in);
int S = nextInt(in);// 开始点的编号
isVisited = new boolean[N+1]; // 节点是不是被访问
result = new int [N+1]; //存放结果
Arrays.fill(isVisited,false);
Arrays.fill(result, Integer.MAX_VALUE);
edges = new ArrayList [N+1];
for(int i = 0;i <= N;i ++){
edges[i] = new ArrayList<Edge>();
}
for(int i = 1;i <= M ;i++){
int u = nextInt(in);
int v = nextInt(in);
int w = nextInt(in);
// 有向图 从 u 到 v 的距离是 w
edges[u].add(new Edge(v,w));
}
/* result[S] = 0; // 出发点到自己的距离为0
PriorityQueue<Edge> pq = new PriorityQueue<Edge>();
pq.add(new Edge(S,0));
while(!pq.isEmpty()){
Edge cur = pq.poll();
if(!isVisited[cur.index]){// 当前点没被访问
isVisited[cur.index] = true;
for(int i = 0 ;i < edges[cur.index].size();i++ ){
Edge nextEdge = edges[cur.index].get(i);
if(!isVisited[nextEdge.index]){
if(result[nextEdge.index] > result[cur.index] + nextEdge.weight ){
result[nextEdge.index] = result[cur.index] + nextEdge.weight;
pq.add(new Edge(nextEdge.index,result[nextEdge.index]));
}
}
}
}
}*/
dijstra(S);
for(int i = 1;i < N+1; i++){
System.out.print(result[i] +" ");
}
}
private static void dijstra(int start){
result[start] = 0 ; // 自己到自己的距离是0
PriorityQueue<Edge> pq = new PriorityQueue<Edge>();
pq.add(new Edge(start,0));
while(!pq.isEmpty()){
Edge cur = pq.poll();
if(!isVisited[cur.index]){
isVisited[cur.index] = true;
for(int i = 0;i < edges[cur.index].size();i++){
Edge next = edges[cur.index].get(i);
if(result[next.index] > next.weight + result[cur.index]){
result[next.index] = next.weight + result[cur.index];
pq.add(new Edge(next.index,result[next.index]));
}
}
}
}
}
private static int nextInt(StreamTokenizer in) throws Exception {
in.nextToken();
return (int)in.nval;
}
static class Edge implements Comparable<Edge>{
int index;
int weight;
public Edge(int index, int weight) {
super();
this.index = index;
this.weight = weight;
}
public int compareTo(Edge o) {
return this.weight - o.weight;
}
}
}