1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
program bzoj_3571; const inf=maxlongint>>1; type point=record x,y:longint; end; var lx,ly,slack,pair:array[1..70]of longint; vx,vy:array[1..70]of boolean; w,a,b:array[1..70,1..70]of longint; t,n,i,j:longint; pa,pb:point;
function (a,b:longint):longint; begin if a<b then exit(a) else exit(b); end;
function find(x:longint):boolean; var t,y:longint; begin vx[x]:=true; for y:=1 to n do begin if vy[y] then continue; t:=lx[x]+ly[y]-w[x,y]; if t=0 then begin vy[y]:=true; if (pair[y]=0)or(find(pair[y])) then begin pair[y]:=x; exit(true); end; end else if t<slack[y] then slack[y]:=t; end; exit(false); end;
function KM:point; var i,j,d:longint; begin fillchar(pair,sizeof(pair),0); for i:=1 to n do lx[i]:=-inf; fillchar(ly,sizeof(ly),0); for i:=1 to n do for j:=1 to n do if w[i,j]>lx[i] then lx[i]:=w[i,j]; for i:=1 to n do begin fillchar(slack,sizeof(slack),$3f); repeat fillchar(vx,sizeof(vx),false); fillchar(vy,sizeof(vy),false); if find(i) then break; d:=inf; for j:=1 to n do if (not vy[j])and(slack[j]<d) then d:=slack[j]; for j:=1 to n do if vx[j] then dec(lx[j],d); for j:=1 to n do if vy[j] then inc(ly[j],d) else dec(slack[j],d); until false; end; KM.x:=0; KM.y:=0; for i:=1 to n do inc(KM.x,a[pair[i],i]); for i:=1 to n do inc(KM.y,b[pair[i],i]); end;
function solve(l,r:point):longint; var t:point; i,j:longint; begin for i:=1 to n do for j:=1 to n do w[i,j]:=a[i,j]*(r.y-l.y)-b[i,j]*(r.x-l.x); t:=KM; if ((t.x=l.x)and(t.y=l.y))or((t.x=r.x)and(t.y=r.y)) then exit(min(l.x*l.y,r.x*r.y)); exit(min(solve(l,t),solve(t,r))); end;
begin readln(t); for t:=1 to t do begin readln(n); for i:=1 to n do for j:=1 to n do read(a[i,j]); for i:=1 to n do for j:=1 to n do read(b[i,j]); for i:=1 to n do for j:=1 to n do w[i,j]:=-a[i,j]; pa:=KM; for i:=1 to n do for j:=1 to n do w[i,j]:=-b[i,j]; pb:=KM; writeln(solve(pa,pb)); end; end.
|