1179: [Apio2009]Atm

时间:2023-03-08 16:55:45

1179: [Apio2009]Atm

Time Limit: 15 Sec  Memory Limit: 162 MB
Submit: 1629  Solved: 615
[Submit][Status]

Description

1179: [Apio2009]Atm

Input

第一行包含两个整数N、M。N表示路口的个数,M表示道路条数。接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号。接下来N行,每行一个整数,按顺序表示每个路口处的ATM机中的钱数。接下来一行包含两个整数S、P,S表示市中心的编号,也就是出发的路口。P表示酒吧数目。接下来的一行中有P个整数,表示P个有酒吧的路口的编号

Output

输出一个整数,表示Banditji从市中心开始到某个酒吧结束所能抢劫的最多的现金总数。

Sample Input

6 7
1 2
2 3
3 5
2 4
4 1
2 6
6 5
10
12
8
16
1 5
1 4
4
3
5
6

Sample Output

47

HINT

50%的输入保证N, M<=3000。所有的输入保证N, M<=500000。每个ATM机中可取的钱数为一个非负整数且不超过4000。输入数据保证你可以从市中心沿着Siruseri的单向的道路到达其中的至少一个酒吧。

Source

 题解:其实就是个tarjan求出强连通分量,缩个点,然后直接BFS扫一遍就完事了。。。但是问题来了,莫名其妙的WA了好多次,弄到数据才发现原来tarjan爆栈了(HansBug:TT)。。。后来加了个{$M 1000000000,0,maxlongint}就没事啦么么哒(再次对C/C++党的无限栈空间表示严重鄙视TT)
 /**************************************************************
Problem:
User: HansBug
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ {$M 1000000000,0,maxlongint}
type
point=^node;
node=record
g:longint;
next:point;
end;
art=array[..] of point;
var
i,j,k,l,m,n,h,t,ans,x,y:longint;
a,d:art;
p:point;
ss,s:array[..] of boolean;
low,dfn,f,b,c,e,g:array[..] of longint;
function min(x,y:longint):longint;inline;
begin
if x<y then min:=x else min:=y;
end;
function max(x,y:longint):longint;inline;
begin
if x>y then max:=x else max:=y;
end;
procedure add(var a:art;x,y:longint);INLINE;
var p:point;
begin
new(p);
p^.g:=y;
p^.next:=a[x];
a[x]:=p;
end;
procedure tarjan(x:longint);inline;
var i,j,k:longint;p:point;
begin
inc(h);low[x]:=h;dfn[x]:=h;
inc(t);f[t]:=x;ss[x]:=true;s[x]:=true;
p:=a[x];
while p<>nil do
begin
if s[p^.g]=false then
begin
tarjan(p^.g);
low[x]:=min(low[x],low[p^.g]);
end
else if ss[p^.g] then low[x]:=min(low[x],dfn[p^.g]);
p:=p^.next;
end;
if dfn[x]=low[x] then
begin
inc(ans);
while f[t+]<>x do
begin
ss[f[t]]:=false;
b[f[t]]:=ans;
dec(t);
end;
end;
end;
procedure search(x:longint);inline;
var
p:point;
f,r,i,j,k:longint;
b:array[..] of longint;
begin
f:=;r:=;
b[]:=x;
while f<r do
begin
p:=a[b[f]];
while p<>nil do
begin
if (e[b[f]]+c[p^.g])>e[p^.g] then
begin
e[p^.g]:=e[b[f]]+c[p^.g];
b[r]:=p^.g;
inc(r);
end;
p:=p^.next;
end;
inc(f);
end;
end;
begin
read(n,m);
for i:= to n do a[i]:=nil;
for i:= to m do
begin
read(j,k);
add(a,j,k);
end;
fillchar(s,sizeof(s),false);
fillchar(ss,sizeof(ss),false);
fillchar(f,sizeof(f),);
fillchar(low,sizeof(low),);
fillchar(dfn,sizeof(dfn),);
fillchar(b,sizeof(b),);
fillchar(c,sizeof(c),); for i:= to n do
read(g[i]);
read(x,m);
ans:=;h:=;t:=;
tarjan(x);
x:=b[x];
for i:= to n do d[i]:=a[i];
for i:= to n do a[i]:=nil;
for i:= to n do c[b[i]]:=c[b[i]]+g[i];
fillchar(s,sizeof(s),false);
for i:= to n do
begin
p:=d[i];
while p<> nil do
begin
if b[i]<>b[p^.g] then add(a,b[i],b[p^.g]);
p:=p^.next;
end;
end; fillchar(e,sizeof(e),);
e[x]:=c[x];
search(x);k:=;
for i:= to m do
begin
read(j);
k:=max(k,e[b[j]]);
end;
writeln(k);
readln;
readln;
end.