var
dist,fa,q:array[1..100] of longint;
cost:array[1..100,1..100] of longint;
f:array[1..100] of boolean;
n,m,r,i,j,a,b,c,p:longint;
procedure spfa(x:longint);
var
h,t,i:longint;
begin
for i:=1 to n do dist[i]:=maxlongint;
h:=0;t:=1;
q[1]:=r;
f[r]:=true;
dist[r]:=0;
fillchar(f,sizeof(f),false);
while h<>t do begin
inc(h);
f[q[h]]:=false;
for i:=1 to n do
if (dist[i]>dist[q[h]]+cost[q[h],i]) and (cost[q[h],i]<>0) then begin
dist[i]:=dist[q[h]]+cost[q[h],i];
fa[i]:=q[h];
if not f[i] then begin
f[i]:=true;
inc(t);
q[t]:=i;
end;
end;
end;
end;
begin
readln(n,m,r);
for i:=1 to m do begin
readln(a,b,c);
cost[a,b]:=c;
end;
spfa(r);
for i:=1 to n do begin
writeln(dist[i]);
p:=i;
write(i);
while fa[p]<>0 do begin
write('<---',fa[p]);
p:=fa[p];
end;
writeln;
end;
readln;
end.