var long,cnt:array[0..10010] of longint; ask:array[0..10010] of boolean; value,reach,next,heap,node:array[0..500010] of longint; n,m,start,head,tail,num:longint;
procedureinsert(x:longint); var t,father:longint; begin if x=1then exit; father:=x div2; if heap[father]>heap[x] then begin t:=heap[x]; heap[x]:=heap[father]; heap[father]:=t; t:=node[x]; node[x]:=node[father]; node[father]:=t; insert(father); end; end;
proceduredown(x:longint); var son:longint; t:longint; begin if x*2>num then exit; son:=x*2; if (son+1<=num)and(heap[son+1]<=heap[son]) then inc(son); if (heap[x]>heap[son]) then begin t:=heap[x]; heap[x]:=heap[son]; heap[son]:=t; t:=node[x]; node[x]:=node[son]; node[son]:=t; end; down(son); end;
procedureread_; var i,l,r,sum:longint; begin filldword(cnt,sizeof(cnt) div4,maxlongint*2+1); filldword(long,sizeof(long) div4,maxlongint);
read(n,m,start); for i:=1to m do begin read(l,r,sum); reach[i]:=r; value[i]:=sum; next[i]:=cnt[l]; cnt[l]:=i; end;
long[start]:=0; end;
procedureDijkstar; var i,node_,minlong,keynode:longint; begin node_:=0; for i:=1to n do begin inc(num); node[num]:=i; heap[num]:=long[i]; insert(num); end;
if ask[keynode]=False then begin inc(node_); ask[keynode]:=True; i:=cnt[keynode]; while i<>-1do begin if long[reach[i]]>value[i]+long[keynode] then begin long[reach[i]]:=value[i]+long[keynode];
inc(num); heap[num]:=long[reach[i]]; node[num]:=reach[i]; insert(num); end; i:=next[i]; end; end; until node_=n; for i:=1to n do write(long[i],' '); end;