P1179: [Apio2009]Atm

时间:2023-12-25 13:43:13

缩点+spfa最短路,因为最终不可能有环,所以直接spfa。

 const maxe=;
type
node=record
f,t:longint;
end;
var n,m,s,i,j,ans,cnt,num,u,x,dgr:longint;
h,he,dfn,low,q,d,v,va,bl:array[..maxe] of longint;
b,bi:array[..maxe] of node;
f:array[..maxe*] of longint;
p:array[..maxe] of boolean;
function min(a,b:longint):longint;
begin
if a>b then exit(b) else exit(a);
end;
procedure insert(u,v:longint);
begin
with bi[i] do
begin
f:=he[u];
t:=v;
end;
he[u]:=i;
end;
procedure reinsert(u,v:longint);
begin
inc(cnt);
with b[cnt] do
begin
f:=h[u];
t:=v;
end;
h[u]:=cnt;
end;
procedure readd;
var i,e,v:longint;
begin
cnt:=;
for i:= to n do
begin
e:=he[i];
while e<> do
begin
v:=bi[e].t;
if bl[i]<>bl[v] then reinsert(bl[i],bl[v]);
e:=bi[e].f;
end;
end;
end;
procedure tarjan(x:longint);
var e,t,j:longint;
begin
inc(dgr); inc(num);
p[x]:=true; f[num]:=x; dfn[x]:=dgr; low[x]:=dgr;
e:=he[x];
while e<> do
begin
t:=bi[e].t;
if dfn[t]= then
begin
tarjan(t);
if low[x]>low[t] then low[x]:=low[t];
end
else if (p[t]) and (dfn[t]<low[x]) then low[x]:=dfn[t];
e:=bi[e].f;
end;
if dfn[x]=low[x] then
begin
j:=; inc(cnt);
while j<>x do
begin
j:=f[num];
dec(num);
p[j]:=false;
bl[j]:=cnt;
//writeln(j,' ',cnt);
inc(v[cnt],va[j]);
end;
end;
end;
procedure spfa;
var e,t,now,l,r:longint;
begin
fillchar(p,sizeof(p),true);
fillchar(d,sizeof(d),);
l:=; r:=; f[]:=bl[s]; p[bl[s]]:=false; d[bl[s]]:=v[bl[s]]; //writeln(v[bl[s]]);
while l<=r do
begin
now:=f[l];
e:=h[now];
while e<> do
begin
t:=b[e].t;
//if t=1 then writeln('x',now,' ',v[t]);
if d[t]<d[now]+v[t] then
begin
d[t]:=d[now]+v[t];
if p[t] then
begin
p[t]:=false;
inc(r);
f[r]:=t;
end;
end;
e:=b[e].f;
end;
inc(l);
p[now]:=true;
end;
end;
begin
readln(n,m);
for i:= to m do
begin
readln(u,x);
insert(u,x);
end;
for i:= to n do readln(va[i]);
fillchar(p,sizeof(p),false);
for i:= to n do if dfn[i]= then tarjan(i);
readln(s,m);
readd;
//for i:=1 to 4 do writeln(v[i]);
spfa;
for i:= to m do
begin
read(u);
if d[bl[u]]>ans then ans:=d[bl[u]];
end;
writeln(ans);
end.

(转载请注明出处:http://www.cnblogs.com/Kalenda/)