您的位置:首页 > 大数据 > 人工智能

PKU1273 Drainage Ditches

2010-11-14 16:45 253 查看
网络流一道!
以前回的是SAP,估计现在让我写得卡死,于是我就把从前都不屑一顾的Edmonds-Karp试了一下。不就是一顿BFS么!我估计NOIP要考什么网络流也就这水平了…用不到什么SAP啦,HLPP什么的。当然模型也会很裸!

program ditch;
const
maxn=250;
inf=10000000;
var
c:array[0..maxn,0..maxn] of longint;
que,tmp,pre:array[0..maxn] of longint;
n,m,delta,ans,head,tail:longint;
procedure prework;
begin
ans:=0;
fillchar(c,sizeof(c),0);
end;
procedure init;
var
i,a,b,w:longint;
begin
readln(m,n);
for i:=1 to m do
begin
readln(a,b,w);
inc(c[a,b],w);
end;
end;
function getmin(a,b:longint):longint;
begin
if a<b then
exit(a);
exit(b);
end;
function bfs(s,t:longint):boolean;
var
u,v:longint;
begin
while head<tail do
begin
inc(head);
u:=que[head];
for v:=1 to n do
if (tmp[v]=0) and (c[u,v]>0) then
begin
inc(tail);
que[tail]:=v;
pre[v]:=u;
tmp[v]:=getmin(tmp[u],c[u,v]);
if v=t then
begin
delta:=tmp[v];
exit(true);
end;
end;
end;
exit(false);
end;
procedure modify(s,t:longint);
var
u,v:longint;
begin
inc(ans,delta);
v:=t;
u:=pre[v];
repeat
dec(c[u,v],delta);
inc(c[v,u],delta);
v:=u;
u:=pre[u];
until v=s;
end;
procedure ek(s,t:longint);
begin
while true do
begin
fillchar(tmp,sizeof(tmp),0);
que[1]:=s;
tmp[s]:=inf;
head:=0;
tail:=1;
delta:=inf;
if not bfs(s,t) then
break;
modify(s,t);
end;
end;
procedure outit;
begin
writeln(ans);
end;
begin
assign(input,'a.in');
reset(input);
assign(output,'a.out');
rewrite(output);
while not eof do
begin
prework;
init;
ek(1,n);
outit;
end;
close(input);
close(output);
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: