很久以前写的,忘补解题报告了
首先似乎dfs就可以了吧?
但还有更高大上的做法
其实这东西就是告诉sum[y]-sum[x-1]=z
然后给出一堆看成不成立
可以用并查集,维护每个点到father点的差即可

 1 var sum,fa:array[0..1010] of longint;
 2     i,t,n,m,x,y,z,k1,k2:longint;
 3     ch:boolean;
 4 function getf(x:longint):longint;
 5   var k:longint;
 6   begin
 7     if fa[x]=x then exit(x)
 8     else begin
 9       k:=fa[x];
10       fa[x]:=getf(fa[x]);
11       sum[x]:=sum[x]+sum[k];
12       exit(fa[x]);
13     end;
14   end;
15 
16 begin
17   readln(t);
18   while t>0 do
19   begin
20     readln(n,m);
21     for i:=0 to n do
22       fa[i]:=i;
23     ch:=true;
24     fillchar(sum,sizeof(sum),0);
25     for i:=1 to m do
26     begin
27       readln(x,y,z);
28       if not ch then continue;
29       dec(x);
30       k1:=getf(x);
31       k2:=getf(y);
32       if (k1<>k2) then
33       begin
34         fa[k1]:=k2;
35         sum[k1]:=sum[y]-sum[x]+z;
36       end
37       else if sum[x]-sum[y]<>z then
38       begin
39         ch:=false;
40         break;
41       end;
42     end;
43     if ch then writeln('true') else writeln('false');
44     dec(t);
45   end;
46 end.
View Code

 

posted on 2015-01-31 23:30  acphile  阅读(185)  评论(0编辑  收藏  举报