标签:
第1行输入N,之后N行每行描述一条水管,前两个英文字母表示水管的两端(大小写字母是不一样的),后一个整数表示水管的流量,流量不会超过1000.
一个整数,表示总流量.
题解:WA了6次再读题才发现大小写字母不一样TT。别的没了,直接上sap模板A之。。
1 type 2 point=^node; 3 node=record 4 g,w:longint; 5 next,anti:point; 6 end; 7 var 8 i,j,k,l,m,n,ans,s,t:longint; 9 a:array[0..100] of point; 10 d,dv:array[0..100] of longint; 11 c1,c2,c3:char; 12 function min(x,y:longint):longint;inline; 13 begin 14 if x<y then min:=x else min:=y; 15 end; 16 procedure add(x,y,z:longint);inline; 17 var p:point; 18 begin 19 new(p);p^.g:=y;p^.w:=z;p^.next:=a[x];a[x]:=p; 20 new(p);p^.g:=x;p^.w:=0;p^.next:=a[y];a[y]:=p; 21 a[y]^.anti:=a[x];a[x]^.anti:=a[y]; 22 end; 23 function dfs(x,flow:longint):longint;inline; 24 var p:point;k:longint; 25 begin 26 if x=t then exit(flow); 27 dfs:=0;p:=a[x]; 28 while p<>nil do 29 begin 30 if (P^.w>0) and (d[x]=(d[p^.g]+1)) then 31 begin 32 k:=dfs(p^.g,min(p^.w,flow-dfs)); 33 dec(p^.w,k); 34 inc(p^.anti^.w,k); 35 inc(dfs,k); 36 if dfs=flow then exit; 37 end; 38 p:=p^.next; 39 end; 40 if d[s]=n then exit; 41 dec(dv[d[x]]); 42 if dv[d[x]]=0 then d[s]:=n; 43 inc(d[x]); 44 inc(dv[d[x]]); 45 end; 46 function trans(x:char):longint;inline; 47 begin 48 case x of 49 ‘A‘..‘Z‘:exit(ord(x)-ord(‘A‘)+1); 50 ‘a‘..‘z‘:exit(ord(x)-ord(‘a‘)+27); 51 end; 52 end; 53 function getc:longint;inline; 54 begin 55 repeat 56 read(c1); 57 until (upcase(c1)>=‘A‘) and (upcase(c1)<=‘Z‘); 58 exit(trans(c1)); 59 end; 60 begin 61 readln(m);n:=52; 62 for i:=1 to n do a[i]:=nil; 63 s:=1;t:=26; 64 for i:=1 to m do 65 begin 66 j:=getc;k:=getc;readln(l); 67 add(j,k,l); 68 end; 69 fillchar(d,sizeof(d),0); 70 fillchar(dv,sizeof(dv),0); 71 dv[0]:=n; 72 ans:=0; 73 while d[s]<n do inc(ans,dfs(s,maxlongint)); 74 writeln(ans); 75 end.
3396: [Usaco2009 Jan]Total flow 水流
标签:
原文地址:http://www.cnblogs.com/HansBug/p/4297728.html