宋壬初吧 关注:25贴子:419
  • 1回复贴,共1
//SRC be locked
var i,j,k,l,m,n,p,q,r,s,t:longint;
    root,len:array[1..500] of longint;
//SRC be locked
function f(x:longint):longint;
var s:longint;
begin
    if root[x]=x then exit(x);
    s:=root[x];
    root[x]:=f(root[x]);
    len[x]:=len[x]+len[s];
    exit(root[x]);
end;
//SRC be locked
begin
    read(n,p);
    for i:=1 to n do root[i]:=i;
    for i:=1 to p do
    begin
        read(l,r,t);
        if f(l)=f(r) then
        begin
            if len[r]-len[l]<>t then
            begin
                writeln(-1);
                halt;
            end;
        end else
        begin
            len[root[r]]:=t+len[l]-len[r];
            root[root[r]]:=root[l];
        end;
    end;
    for i:=1 to n do root[i]:=f(i);
    k:=0;
    for i:=1 to n do if len[i]<k then k:=len[i];
    for i:=1 to n do len[i]:=len[i]-k;
    for i:=1 to n do writeln(len[i]);
end.
//SRC be locked


1楼2009-10-08 20:25回复
    回忆Vijos健在的时光


    2楼2010-08-06 00:27
    回复