bzoj 1458 最大流

来源:互联网 发布:淘宝宠物用品店铺介绍 编辑:程序博客网 时间:2024/05/22 15:11

题意:有一个M * N的棋盘,有的格子是障碍。现在你要选择一些格子来放置一些士兵,一个格子里最多可以放置一个士兵,障碍格里不能放置士兵。我们称这些士兵占领了整个棋盘当满足第i行至少放置了Li个士兵, 第j列至少放置了Cj个士兵。现在你的任务是要求使用最少个数的士兵来占领整个棋盘。


题目要求用的最少,转化一下即不用的最多

即 先放满棋盘,然后考虑最多可以拿走多少个

最大流,初始化答案为所有可以放的格子数

显然,当某一行或某一列的可以放的格子数小于需求就直接jiong掉

考虑,由于每在(x,y)放置一个士兵,对第x行和第y列都会有贡献

建图:

S -> 每一行 容量为可以放的格子数- 最少放置格子数 (即容量为第i行最多可以拿走的数量)

每一列 -> T 容量为可以放的格子数- 最少放置格子数(即容量为第j列最多可以拿走的数量)

每一个可以放的格子(i,j)  i -> j 容量为1

最终答案为 ans-maxflow

var        n,m,l,x,y,k     :longint;        ss,st,ans       :longint;        i,j             :longint;        flag            :array[0..110,0..110] of boolean;        que,dis,last,a,b:array[0..210] of longint;        pre,other,len   :array[0..20505] of longint;function min(a,b:longint):longint;begin   if a<b then exit(a) else exit(b);end;procedure connect(x,y,z:longint);begin   inc(l);   pre[l]:=last[x];   last[x]:=l;   other[l]:=y;   len[l]:=z;end;function bfs:boolean;var        h,tl,i,p,q,cur:longint;begin   fillchar(dis,sizeof(dis),0);   h:=0; tl:=1; que[1]:=ss; dis[ss]:=1;   while (h<>tl) do   begin      h:=h mod 205+1;      cur:=que[h];      q:=last[cur];      while (q<>0) do      begin         p:=other[q];         if (dis[p]=0) and (len[q]>0) then         begin            dis[p]:=dis[cur]+1;            tl:=tl mod 205+1;            que[tl]:=p;            if p=st then exit(true);         end;         q:=pre[q];      end;   end;   exit(false);end;function dinic(x,flow:longint):longint;var        rest,tt,p,q:longint;begin   if x=st then exit(flow);   rest:=flow;   q:=last[x];   while (q<>0) do   begin      p:=other[q];      if (dis[p]=dis[x]+1) and (len[q]>0) and (rest>0) then      begin         tt:=dinic(p,min(rest,len[q]));         dec(len[q],tt);         inc(len[q xor 1],tt);         dec(rest,tt);         if rest=0 then exit(flow);      end;      q:=pre[q];   end;   if rest=flow then dis[x]:=0;   exit(flow-rest);end;begin   read(n,m,k);   l:=1; ans:=n*m-k; ss:=n+m+1; st:=ss+1;   for i:=1 to n do   begin      read(x); a[i]:=m-x;   end;   for i:=1 to m do   begin      read(x); b[i]:=n-x;   end;   //   for i:=1 to k do   begin      read(x,y);      dec(a[x]); dec(b[y]); flag[x,y]:=true;      if (a[x]<0) or (b[y]<0) then      begin         writeln('JIONG!');exit;      end;   end;   //   for i:=1 to n do   begin      connect(ss,i,a[i]);      connect(i,ss,0);   end;   for i:=1 to m do   begin      connect(n+i,st,b[i]);      connect(st,n+i,0);   end;   for i:=1 to n do     for j:=1 to m do       if not flag[i,j] then       begin          connect(i,n+j,1);          connect(n+j,i,0);       end;   //   while bfs do dec(ans,dinic(ss,maxlongint div 10));    writeln(ans);end.
——by Eirlys



0 0
原创粉丝点击