您的位置:首页 > 其它

codevs1034

2015-07-18 15:43 405 查看
题目地址:http://codevs.cn/problem/1034/

代码:

type ss=record
y,c,r,next:longint;
end;
const maxn=10100; maxm=100000; oo=maxlongint shr 2;
var n,m,i,j,k,l,st,ed,t1,t2,t,tot,limit,ans,augo:longint;
v:array[0..30] of boolean;
ee:array[0..30,0..30] of boolean;
e:array[0..maxm] of ss;
r,z:array[0..31] of longint;
p:array[0..31,0..100] of longint;
h,g,b,d:array[0..maxn] of longint;
can:boolean;
procedure shi(k:longint);
var i:longint;
begin
if v[k] then exit;
v[k]:=true;
for i:=1 to n do if ee[k,i] then shi(i);
if v
then exit;
end;
procedure jia(x,y,c:longint);
begin
inc(tot); e[tot].y:=y; e[tot].c:=c; e[tot].next:=b[x]; b[x]:=tot;
inc(tot); e[tot].y:=x; e[tot].next:=b[y]; b[y]:=tot;
e[tot].r:=tot-1; e[tot-1].r:=tot;
end;
procedure flow(k:longint);
var i,temp:longint;
begin
temp:=augo;
if k=ed then
begin
can:=true;
inc(ans,augo);
exit;
end;
i:=d[k];
while i<>-1 do
begin
if e[i].c>0 then
begin
if h[e[i].y]+1=h[k] then
begin
if e[i].c<augo then augo:=e[i].c;
flow(e[i].y);
if h[st]>=limit then exit;
if can then break;
end;
augo:=temp;
end;
i:=e[i].next;
end;
if can then
begin
d[k]:=i; dec(e[i].c,augo); inc(e[e[i].r].c,augo);
end
else
begin
d[k]:=b[k]; dec(g[h[k]]);
if g[h[k]]<=0 then h[st]:=limit;
inc(h[k]); inc(g[h[k]]);
end;
end;
begin
readln(n,m,k);
for i:=1 to m do
begin
read(r[i],z[i]);
for j:=0 to z[i]-1 do
begin
read(p[i,j]);
if p[i,j]=0 then p[i,j]:=n+1;
if p[i,j]=-1 then p[i,j]:=n+2;
end;
for j:=0 to z[i]-1 do
for l:=0 to z[i]-1 do ee[p[i,j],p[i,l]]:=true;
end;
inc(n,2); shi(n-1);
if not(v
) then writeln(0)
else
begin
st:=1000; ed:=1001;
fillchar(b,sizeof(b),255);
jia(st,n-1,k); jia(n,ed,oo);
repeat
inc(t);
jia(st,n-1+t*n,k); jia(n+t*n,ed,oo);
for i:=1 to n do jia(i+(t-1)*n,i+t*n,oo);
for i:=1 to m do
begin
t1:=p[i,(t-1) mod z[i]]+(t-1)*n;
t2:=p[i,t mod z[i]]+t*n;
jia(t1,t2,r[i]);
end;
limit:=(t+1)*n+2;
fillchar(h,sizeof(h),0);
fillchar(g,sizeof(g),0);
g[0]:=limit;
for i:=1 to limit do d[i]:=b[i];
d[st]:=b[st]; d[ed]:=b[ed];
while h[st]<limit do
begin
can:=false;
augo:=oo;
flow(st);
end;
until ans>=k;
writeln(t);
end;
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: