【UVA11082】Matrix Decompressing(有上下界的网络流)

题意:给出一个矩阵前i列所有元素的和,和前j行所有元素的和,求这个矩阵解压以后的原型。(答案不唯一)

n,m<=20,1<=a[i,j]<=20

思路:这道题把边上的流量作为原先矩阵中的点

把每一行,每一列都看成一个点

S——>i行 a[i]-m

i行——>j列 19

j列——>T b[i]-n

跑最大流,边(i,j+n)上的流量就是a[i,j]的值

为什么容量是a[i]-m,19,b[i]-n?

因为点权(边权)不能为0,所以要先把所有值+1,上限就-1,输出的时候+1即可

  1 var fan:array[1..200000]of longint;
  2     head,vet,next,len,gap,dis,a,b,c1,c2:array[0..50000]of longint;
  3     num:array[1..30,1..30]of longint;
  4     n,m,i,j,tot,s,source,src,cas,v:longint;
  5 
  6 procedure add(a,b,c:longint);
  7 begin
  8  inc(tot);
  9  next[tot]:=head[a];
 10  vet[tot]:=b;
 11  len[tot]:=c;
 12  head[a]:=tot;
 13 
 14  inc(tot);
 15  next[tot]:=head[b];
 16  vet[tot]:=a;
 17  len[tot]:=0;
 18  head[b]:=tot;
 19 end;
 20 
 21 function min(x,y:longint):longint;
 22 begin
 23  if x<y then exit(x);
 24  exit(y);
 25 end;
 26 
 27 function dfs(u,aug:longint):longint;
 28 var e,v,t,flow,val:longint;
 29 begin
 30  if u=src then exit(aug);
 31  e:=head[u]; flow:=0; val:=s-1;
 32  while e<>0 do
 33  begin
 34   v:=vet[e];
 35   if len[e]>0 then
 36   begin
 37    if dis[u]=dis[v]+1 then
 38    begin
 39     t:=dfs(v,min(len[e],aug-flow));
 40     len[e]:=len[e]-t;
 41     len[fan[e]]:=len[fan[e]]+t;
 42     flow:=flow+t;
 43     if dis[source]>=s then exit(flow);
 44     if aug=flow then break;
 45    end;
 46    val:=min(val,dis[v]);
 47   end;
 48   e:=next[e];
 49  end;
 50  if flow=0 then
 51  begin
 52   dec(gap[dis[u]]);
 53   if gap[dis[u]]=0 then dis[source]:=s;
 54   dis[u]:=val+1;
 55   inc(gap[dis[u]]);
 56  end;
 57  exit(flow);
 58 end;
 59 
 60 function maxflow:longint;
 61 var ans:longint;
 62 begin
 63  fillchar(gap,sizeof(gap),0);
 64  fillchar(dis,sizeof(dis),0);
 65  gap[0]:=s; ans:=0;
 66  while dis[source]<s do ans:=ans+dfs(source,maxlongint);
 67  exit(ans);
 68 end;
 69 
 70 begin
 71  assign(input,'UVA11082.in'); reset(input);
 72  assign(output,'UVA11082.out'); rewrite(output);
 73  for i:=1 to 200000 do
 74   if i mod 2=1 then fan[i]:=i+1
 75    else fan[i]:=i-1;
 76  readln(cas);
 77  for v:=1 to cas do
 78  begin
 79   fillchar(head,sizeof(head),0); tot:=0;
 80   fillchar(num,sizeof(num),0);
 81   fillchar(len,sizeof(len),0);
 82   readln(n,m);
 83   for i:=1 to n do
 84   begin
 85    read(a[i]); c1[i]:=a[i]-a[i-1];
 86   end;
 87   for i:=1 to m do
 88   begin
 89    read(b[i]); c2[i]:=b[i]-b[i-1];
 90   end;
 91   s:=n+m+2; source:=n+m+1; src:=n+m+2;
 92   for i:=1 to n do add(source,i,c1[i]-m);
 93   for i:=1 to m do add(i+n,src,c2[i]-n);
 94   for i:=1 to n do
 95    for j:=1 to m do
 96    begin
 97     num[i,j]:=tot+2;
 98     add(i,j+n,19);
 99    end;
100   maxflow;
101   writeln('Matrix ',v);
102   for i:=1 to n do
103   begin
104    for j:=1 to m-1 do write(len[num[i,j]]+1,' ');
105    write(len[num[i,m]]+1);
106    writeln;
107   end;
108   if v<>cas then writeln;
109  end;
110 
111  close(input);
112  close(output);
113 end.
原文地址:https://www.cnblogs.com/myx12345/p/6230764.html