显然是有源有汇有下界最大流,不刷不知道,一刷吓一跳
发现了我之前求有源有汇有下界最大流的错误,具体见我那篇介绍有下界的网络流的解题报告(bzoj2502),已经更正
1 const inf=10000007; 2 type node=record 3 po,next,flow:longint; 4 end; 5 6 var e:array[0..40010] of node; 7 pre,p,numh,h,cur,d:array[0..220] of longint; 8 a:array[0..110,0..110] of double; 9 len,tot,i,j,n,s,t,ss,tt:longint; 10 11 function min(a,b:longint):longint; 12 begin 13 if a>b then exit(b) else exit(a); 14 end; 15 16 procedure add(x,y,f:longint); 17 begin 18 inc(len); 19 e[len].po:=y; 20 e[len].flow:=f; 21 e[len].next:=p[x]; 22 p[x]:=len; 23 end; 24 25 procedure build(x,y,f:longint); 26 begin 27 add(x,y,f); 28 add(y,x,0); 29 end; 30 31 function sap(s,t:longint):longint; 32 var u,neck,tmp,i,j,q:longint; 33 begin 34 fillchar(numh,sizeof(numh),0); 35 fillchar(h,sizeof(h),0); 36 for i:=0 to tt do 37 cur[i]:=p[i]; 38 u:=s; 39 sap:=0; 40 numh[0]:=tt+1; 41 neck:=inf; 42 while h[s]<tt+1 do 43 begin 44 d[u]:=neck; 45 i:=cur[u]; 46 while i<>-1 do 47 begin 48 j:=e[i].po; 49 if (e[i].flow>0) and (h[u]=h[j]+1) then 50 begin 51 neck:=min(neck,e[i].flow); 52 cur[u]:=i; 53 pre[j]:=u; 54 u:=j; 55 if u=t then 56 begin 57 sap:=sap+neck; 58 while u<>s do 59 begin 60 u:=pre[u]; 61 j:=cur[u]; 62 dec(e[j].flow,neck); 63 inc(e[j xor 1].flow,neck); 64 end; 65 neck:=inf; 66 end; 67 break; 68 end; 69 i:=e[i].next; 70 end; 71 if i=-1 then 72 begin 73 dec(numh[h[u]]); 74 if numh[h[u]]=0 then exit; 75 q:=-1; 76 tmp:=tt; 77 i:=p[u]; 78 while i<>-1 do 79 begin 80 j:=e[i].po; 81 if e[i].flow>0 then 82 if h[j]<tmp then 83 begin 84 q:=i; 85 tmp:=h[j]; 86 end; 87 i:=e[i].next; 88 end; 89 cur[u]:=q; 90 h[u]:=tmp+1; 91 inc(numh[h[u]]); 92 if u<>s then 93 begin 94 u:=pre[u]; 95 neck:=d[u]; 96 end; 97 end; 98 end; 99 end; 100 101 begin 102 len:=-1; 103 fillchar(p,sizeof(p),255); 104 readln(n); 105 dec(n); 106 s:=0; t:=2*n+1; ss:=2*n+2; tt:=2*n+3; 107 for i:=1 to n+1 do 108 for j:=1 to n+1 do 109 read(a[i,j]); 110 for i:=1 to n do 111 begin 112 if a[i,n+1]<>trunc(a[i,n+1]) then 113 build(s,i,1); 114 d[i]:=d[i]+trunc(a[i,n+1]); 115 d[s]:=d[s]-trunc(a[i,n+1]); 116 if a[n+1,i]<>trunc(a[n+1,i]) then 117 build(i+n,t,1); 118 d[i+n]:=d[i+n]-trunc(a[n+1,i]); 119 d[t]:=d[t]+trunc(a[n+1,i]); 120 end; 121 for i:=1 to n do 122 for j:=1 to n do 123 begin 124 if trunc(a[i,j])<>a[i,j] then build(i,j+n,1); 125 d[i]:=d[i]-trunc(a[i,j]); 126 d[j+n]:=d[j+n]+trunc(a[i,j]); 127 end; 128 for i:=s to t do 129 if d[i]>0 then 130 begin 131 build(ss,i,d[i]); 132 tot:=tot+d[i]; 133 end 134 else if d[i]<0 then build(i,tt,-d[i]); 135 build(t,s,inf); 136 if sap(ss,tt)<>tot then writeln('No') 137 else writeln(sap(s,t)*3); 138 end.