3396: [Usaco2009 Jan]Total flow 水流

时间:2022-12-08 07:04:05

3396: [Usaco2009 Jan]Total flow 水流

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 179  Solved: 73
[Submit][Status]

Description

 3396: [Usaco2009 Jan]Total flow 水流
3396: [Usaco2009 Jan]Total flow 水流

Input

第1行输入N,之后N行每行描述一条水管,前两个英文字母表示水管的两端(大小写字母是不一样的),后一个整数表示水管的流量,流量不会超过1000.

Output

一个整数,表示总流量.

Sample Input

5
A B 3
B C 3
C D 5
D Z 4
B Z 6

Sample Output

3

HINT

 

Source

Silver

题解:WA了6次再读题才发现大小写字母不一样TT。别的没了,直接上sap模板A之。。

 type
point=^node;
node=record
g,w:longint;
next,anti:point;
end;
var
i,j,k,l,m,n,ans,s,t:longint;
a:array[..] of point;
d,dv:array[..] of longint;
c1,c2,c3:char;
function min(x,y:longint):longint;inline;
begin
if x<y then min:=x else min:=y;
end;
procedure add(x,y,z:longint);inline;
var p:point;
begin
new(p);p^.g:=y;p^.w:=z;p^.next:=a[x];a[x]:=p;
new(p);p^.g:=x;p^.w:=;p^.next:=a[y];a[y]:=p;
a[y]^.anti:=a[x];a[x]^.anti:=a[y];
end;
function dfs(x,flow:longint):longint;inline;
var p:point;k:longint;
begin
if x=t then exit(flow);
dfs:=;p:=a[x];
while p<>nil do
begin
if (P^.w>) and (d[x]=(d[p^.g]+)) then
begin
k:=dfs(p^.g,min(p^.w,flow-dfs));
dec(p^.w,k);
inc(p^.anti^.w,k);
inc(dfs,k);
if dfs=flow then exit;
end;
p:=p^.next;
end;
if d[s]=n then exit;
dec(dv[d[x]]);
if dv[d[x]]= then d[s]:=n;
inc(d[x]);
inc(dv[d[x]]);
end;
function trans(x:char):longint;inline;
begin
case x of
'A'..'Z':exit(ord(x)-ord('A')+);
'a'..'z':exit(ord(x)-ord('a')+);
end;
end;
function getc:longint;inline;
begin
repeat
read(c1);
until (upcase(c1)>='A') and (upcase(c1)<='Z');
exit(trans(c1));
end;
begin
readln(m);n:=;
for i:= to n do a[i]:=nil;
s:=;t:=;
for i:= to m do
begin
j:=getc;k:=getc;readln(l);
add(j,k,l);
end;
fillchar(d,sizeof(d),);
fillchar(dv,sizeof(dv),);
dv[]:=n;
ans:=;
while d[s]<n do inc(ans,dfs(s,maxlongint));
writeln(ans);
end.