题目说是拓扑图,所以提示是DP,之前跟FK学到逆拓扑这次用了。需要注意的是题目要求一条完整的路,所以初始化为-oo。因为TUOPU写萎了,WA了2次。之前FK那个题因为是无向图,而且是树,而且起点还是root,所以可以直接BFS就得到拓扑序,这个题不行。

Code
uses math;
const maxn=100000;
maxm=11000000;
var a,Q,Adjlist,d:array[1..maxn] of longint;
f:array[1..maxn] of int64;
vis:array[1..maxn] of boolean;
v,next:array[1..maxm] of longint;
count,x,y,i,h,t,tot,n,m:longint;
ans:int64;
procedure build(x,y:longint);
begin
inc(tot);
v[tot]:=y;
next[tot]:=Adjlist[x];
Adjlist[x]:=tot;
end;
procedure TUOPUSort;
var i:longint;
begin
while h<t do begin
inc(h);i:=Adjlist[Q[h]];
while i<>0 do begin
dec(d[v[i]]);
if(not vis[v[i]])and(d[v[i]]=0)
then begin
inc(t);Q[t]:=v[i];
vis[v[i]]:=true;
end;
i:=next[i];end;end;
end;
procedure calc;
var i,u:longint;
begin
for h:=t downto 1 do
begin
i:=Adjlist[Q[h]];
if i=0 then f[Q[h]]:=A[Q[h]]
else f[Q[h]]:=$8fffffffffffffff;
while i<>0 do begin
f[Q[h]]:=max(f[v[i]]+A[Q[h]],f[Q[h]]);
i:=next[i];end;
end;
end;
begin
readln(n,m);ans:=$8fffffffffffffff;h:=0;t:=0;
for i:=1 to n do readln(a[i]);
for i:=1 to m do
begin
readln(x,y);
build(x,y);
inc(d[y]);
end;
for i:=1 to n do
if(d[i]=0)
then begin
inc(t);
Q[t]:=i;
vis[i]:=true;
end;
count:=t;
TUOPUSort;calc;
for i:=1 to count do
ans:=max(f[Q[i]],ans);
writeln(ans);
end.