zoukankan      html  css  js  c++  java
  • 【POJ1149&BZOJ1280】PIGS(最大流)

    题意:Emmy在一个养猪场工作。这个养猪场有M个锁着的猪圈,但Emmy并没有钥匙。

    顾客会到养猪场来买猪,一个接着一个。每一位顾客都会有一些猪圈的钥匙,他们会将这些猪圈打开并买走固定数目的猪。

    所有顾客有的钥匙和他们需要买猪的数量在事先都告诉了Emmy,于是Emmy要订一个计划,使得卖出去的猪最多。

    买卖的过程是这样的:一个顾客前来,并打开所有他可以打开的猪圈。

    然后Emmy从这些猪圈里牵出固定数目的猪卖给顾客(最多只能和顾客需要数相等),并可以重新安排这些开着的猪圈中的猪。

    每个猪圈可以存放任意数目的猪。 写一个程序,使得Emmy能够卖出去尽可能多的猪。

    1 ≤ M ≤ 1000
    1 ≤ N ≤ 100

    思路:RYZ作业

    考虑到如果有两个人拥有同一个猪圈的钥匙,则前一个人可以分配任意(不能超过他自己能拿到上限)数量的猪给后一个人

    前一个人——>后一个人 OO

    又发现他们之间不用全部两两连边,只需要按到来顺序,相邻连边

    猪圈其实就是从源点出来的容量上限

    源点——>某猪圈第一个人 a[i]

    最后还有每个人购买的限制

    每个人——>汇点 b[i]

    也是网络流经典模型之一

      1 var head,vet,next,len,dis,gap,fan:array[0..110000]of longint;
      2     a,last:array[1..10000]of longint;
      3     m,n,i,j,x,y,z,tot,s,source,src:longint;
      4 
      5 procedure add(a,b,c:longint);
      6 begin
      7  inc(tot);
      8  next[tot]:=head[a];
      9  vet[tot]:=b;
     10  len[tot]:=c;
     11  head[a]:=tot;
     12 
     13  inc(tot);
     14  next[tot]:=head[b];
     15  vet[tot]:=a;
     16  len[tot]:=0;
     17  head[b]:=tot;
     18 end;
     19 
     20 function min(x,y:longint):longint;
     21 begin
     22  if x<y then exit(x);
     23  exit(y);
     24 end;
     25 
     26 function dfs(u,aug:longint):longint;
     27 var e,v,t,flow,val:longint;
     28 begin
     29  if u=src then exit(aug);
     30  e:=head[u]; flow:=0; val:=s-1;
     31  while e<>0 do
     32  begin
     33   v:=vet[e];
     34   if len[e]>0 then
     35   begin
     36    if dis[u]=dis[v]+1 then
     37    begin
     38     t:=dfs(v,min(len[e],aug-flow));
     39     len[e]:=len[e]-t;
     40     len[fan[e]]:=len[fan[e]]+t;
     41     flow:=flow+t;
     42     if dis[source]>=s then exit(flow);
     43     if aug=flow then break;
     44    end;
     45    val:=min(val,dis[v]);
     46   end;
     47   e:=next[e];
     48  end;
     49  if flow=0 then
     50  begin
     51   dec(gap[dis[u]]);
     52   if gap[dis[u]]=0 then dis[source]:=s;
     53   dis[u]:=val+1;
     54   inc(gap[dis[u]]);
     55  end;
     56  exit(flow);
     57 end;
     58 
     59 function maxflow:longint;
     60 var ans:longint;
     61 begin
     62  fillchar(gap,sizeof(gap),0);
     63  fillchar(dis,sizeof(dis),0);
     64  gap[0]:=s; ans:=0;
     65  while dis[source]<s do ans:=ans+dfs(source,maxlongint);
     66  exit(ans);
     67 end;
     68 
     69 begin
     70  assign(input,'bzoj1280.in'); reset(input);
     71  assign(output,'bzoj1280.out'); rewrite(output);
     72  readln(m,n);
     73  for i:=1 to m do read(a[i]);
     74  for i:=1 to 110000 do
     75   if i and 1=1 then fan[i]:=i+1
     76    else fan[i]:=i-1;
     77  source:=n+1; src:=n+2; s:=n+2;
     78  for i:=1 to n do
     79  begin
     80   read(x);
     81   for j:=1 to x do
     82   begin
     83    read(y);
     84    if last[y]=0 then
     85    begin
     86     add(source,i,a[y]);
     87     last[y]:=i;
     88    end
     89     else
     90     begin
     91      add(last[y],i,maxlongint);
     92      last[y]:=i;
     93     end;
     94   end;
     95   read(z);
     96   add(i,src,z);
     97  end;
     98  writeln(maxflow);
     99  close(input);
    100  close(output);
    101 end.
  • 相关阅读:
    再谈TextField
    IOS-TextField知多少
    leftBarButtonItems
    LeftBarButtonItems,定制导航栏返回按钮
    Apple Mach-O Linker (id) Error "_OBJC_CLASS...错误解决办法 Apple Mach-O Linker (id) Error "_OBJC_CLASS...错误解决办法
    Unrecognized Selector Sent to Instance问题之诱敌深入关门打狗解决办法
    UNRECOGNIZED SELECTOR SENT TO INSTANCE 问题快速定位的方法
    Present ViewController,模态详解
    UILABEL AUTOLAYOUT自动换行 版本区别
    iOS自动布局解决警告Automatic Preferred Max Layout Width is not available on iOS versions prior to 8.0
  • 原文地址:https://www.cnblogs.com/myx12345/p/6495519.html
Copyright © 2011-2022 走看看