这题就是个拓扑排序,注意题目要求:
1、输入层的c[i]按照输入所给直接传递,不需要用公式-u[i]
(其实也没办法用公式,输入层入度为0,没有边连入)
2、答案是最后状态非0的输出层(出度为0)
3、只有当c[i]>0的时候才向下传递,只有此时对于它所连的点才使用公式
var
n,m,x,y,l,z,h,tl :longint;
cur,p,q :longint;
i :longint;
b :boolean;
last,c,u,indu,outdu :array[0..110] of longint;
vis,shuru :array[0..110] of boolean;
que :array[0..110] of longint;
pre,other,len :array[0..1010] of longint;
procedure connect(x,y,z:longint);
begin
inc(l);
pre[l]:=last[x];
last[x]:=l;
other[l]:=y;
len[l]:=z;
end;
begin
read(n,m);
for i:=1 to n do read(c[i],u[i]);
for i:=1 to m do
begin
read(x,y,z);
connect(x,y,z);
inc(indu[y]);
inc(outdu[x]);
end;
for i:=1 to n do if (indu[i]=0) then
begin
inc(tl);
que[tl]:=i;
vis[i]:=true;
shuru[i]:=true;
end;
h:=0;
//
while (h<>tl) do
begin
h:=h mod 100+1;
cur:=que[h];
vis[cur]:=false;
q:=last[cur];
if not shuru[cur] then c[cur]:=c[cur]-u[cur];
while (q<>0) do
begin
p:=other[q];
dec(indu[p]);
if c[cur]>0 then c[p]:=c[p]+len[q]*c[cur];
if (indu[p]=0) and not vis[p] then
begin
tl:=tl mod 100+1;
vis[p]:=true;
que[tl]:=p;
end;
q:=pre[q];
end;
end;
//
for i:=1 to n do
if (c[i]>0) and (outdu[i]=0) then
begin
writeln(i,' ',c[i]);b:=true;
end;
//
if not b then writeln('NULL');
end.
——by Eirlys转载请注明出处=w=