POJ Optimal Milking 2012-08-28
1 Program poj2112; 2 3 var n,c,m,mid,s,t:longint; 4 5 map,a:array[1..300,1..300]of longint; 6 7 cur,dis:array[1..300]of longint; 8 9 vh:array[0..300]of longint; 10 11 Procedure init; 12 13 var i,j,k:longint; 14 15 begin 16 17 readln(n,c,m); 18 19 for i:=1 to n+c do 20 21 for j:=1 to n+c do 22 23 read(map[i,j]); 24 25 s:=n+c+1;t:=n+c+2; 26 27 end; 28 29 30 Procedure floyed; 31 32 var i,j,k:longint; 33 34 begin 35 36 for k:=1 to n+c do 37 38 for i:=1 to n+c do 39 40 for j:=1 to n+c do 41 42 if (i<>j)and(i<>k)and(j<>k)and(map[i,k]<>0)and(map[k,j]<>0) then 43 44 begin 45 46 if (map[i,j]=0)or(map[i,k]+map[k,j]<map[i,j]) then map[i,j]:=map[i,k]+map[k,j]; 47 48 end; 49 50 end; 51 52 53 Function min(a,b:longint):longint; 54 55 begin 56 57 if a<b then min:=a else min:=b; 58 59 end; 60 61 62 Function aug(x,nf:longint):longint; 63 64 var i,j,d,l,minh,ins:longint; 65 66 begin 67 68 if x=t then exit(nf); 69 70 l:=nf; 71 72 for i:=cur[x] to t do 73 74 if (a[x,i]>0)and(dis[x]=dis[i]+1) then 75 76 begin 77 78 cur[x]:=i; 79 80 d:=aug(i,min(l,a[x,i])); 81 82 dec(a[x,i],d); 83 84 inc(a[i,x],d); 85 86 dec(l,d); 87 88 if (dis[s]=t)or(l=0) then exit(nf-l); 89 90 end; 91 92 if nf=l then 93 94 begin 95 96 minh:=t; 97 98 for i:=1 to t do 99 100 if (a[x,i]>0)and(dis[i]<minh) then 101 102 begin 103 104 minh:=dis[i];ins:=i; 105 106 end; 107 108 cur[x]:=ins; 109 110 dec(vh[dis[x]]); 111 112 if vh[dis[x]]=0 then dis[s]:=t; 113 114 dis[x]:=minh+1; 115 116 inc(vh[dis[x]]); 117 118 end; 119 120 aug:=nf-l; 121 122 end; 123 124 125 Function flow:longint; 126 127 var i,j,k:longint; 128 129 begin 130 131 flow:=0; 132 133 fillchar(vh,sizeof(vh),0); 134 135 vh[0]:=t; 136 137 fillchar(dis,sizeof(dis),0); 138 139 for i:=1 to t do cur[i]:=1; 140 141 fillchar(a,sizeof(a),0); 142 143 for i:=1 to n do 144 145 for j:=n+1 to n+c do 146 147 if (map[i,j]<>0)and(map[i,j]<=mid) then a[i,j]:=1; 148 149 for i:=1 to n do a[s,i]:=m; 150 151 for i:=n+1 to n+c do a[i,t]:=1; 152 153 while dis[s]<t do inc(flow,aug(s,maxlongint)); 154 155 end; 156 157 158 Procedure main; 159 160 var i,j,k,lc,rc,ans:longint; 161 162 begin 163 164 floyed; 165 166 lc:=0;rc:=200000; 167 168 ans:=0; 169 170 while lc<rc-1 do 171 172 begin 173 174 mid:=(lc+rc)div 2; 175 176 if flow=c then begin rc:=mid;ans:=mid;end 177 178 else lc:=mid; 179 180 end; 181 182 writeln(ans); 183 184 end; 185 186 187 Begin 188 189 assign(input,'input.in');assign(output,'output.out'); 190 191 reset(input);rewrite(output); 192 193 init; 194 195 main; 196 197 close(input);close(output); 198 199 end.
_____MildTheorem