非常好的网络流

每个顾客分别用一个结点来表示。

对于每个猪圈的第一个顾客,从源点向他连一条边,容量就是该猪圈里的猪的初始数量

对于每个猪圈,假设有n个顾客打开过它,则对所有整数i∈[1, n),从该猪圈的第i个顾客向第i + 1个顾客连一条边,容量为无穷。

从各个顾客到汇点各有一条边,容量是各个顾客能买的数量上限。

其实很好理解

 const inf=;
type node=record
       from,point,flow,next:longint;
     end;
var p,s,w,h,numh,cur,pre:array[..] of longint;
    v:array[..] of boolean;
    a:array[..,..] of longint;
    edge:array[..] of node;
    ans,z,len,t,i,j,k,x,y,n,m:longint; procedure add(x,y,f:longint);
  begin
    inc(len);
    edge[len].from:=x;
    edge[len].point:=y;
    edge[len].flow:=f;
    edge[len].next:=p[x];
    p[x]:=len;
  end; procedure sap;
  var q,u,i,j,flow,neck,tmp:longint;
  begin
    fillchar(numh,sizeof(numh),);
    fillchar(h,sizeof(h),);
    fillchar(pre,sizeof(pre),);
    numh[]:=t+;
    u:=;
    while h[]<t+ do
    begin
      if u=t then
      begin
        neck:=;
        flow:=inf;
        i:=;
        j:=cur[i];
        while i<>t do
        begin
          if flow>edge[j].flow then
          begin
            flow:=edge[j].flow;
            neck:=i;
          end;
          i:=edge[j].point;
          j:=cur[i];
        end;
        i:=;
        j:=cur[i];
        while i<>t do
        begin
          dec(edge[j].flow,flow);
          inc(edge[j xor ].flow,flow);
          i:=edge[j].point;
          j:=cur[i];
        end;
        ans:=ans+flow;
        u:=neck;
      end;
      q:=-;
      i:=p[u];
      while i<>- do
      begin
        x:=edge[i].point;
        if (edge[i].flow>) and (h[u]=h[x]+) then
        begin
          q:=i;
          break;
        end;
        i:=edge[i].next;
      end;
      if q<>- then
      begin
        cur[u]:=q;
        pre[x]:=u;
        u:=x;
      end
      else begin
        dec(numh[h[u]]);
        if numh[h[u]]= then break;
        tmp:=t+;
        i:=p[u];
        while i<>- do
        begin
          x:=edge[i].point;
          if (edge[i].flow>) then tmp:=min(tmp,h[x]);
          i:=edge[i].next;
        end;
        h[u]:=tmp+;
        inc(numh[h[u]]);
        if u<> then u:=pre[u];
      end;
    end;
  end; begin
  readln(m,n);
  fillchar(p,sizeof(p),);
  len:=-;
  t:=n+;
  for i:= to m do
    read(w[i]);
  for i:= to n do
  begin
    read(y);
    z:=;
    for j:= to y do
    begin
      read(x);
      if v[x]=false then
      begin
        v[x]:=true;
        z:=z+w[x];   //如果多条边连接源点和顾客,那么合并
      end;
      inc(s[x]);
      a[x,s[x]]:=i;
    end;
    if z<> then
    begin
      add(,i,z);
      add(i,,);
    end;
    readln(x);
    add(i,t,x);
    add(t,i,);
  end;
  for i:= to m do
    for j:= to s[i]- do
      for k:=j+ to s[i] do
      begin
        add(a[i,j],a[i,k],inf);
        add(a[i,k],a[i,j],);
      end;
  sap;
  writeln(ans);
end.

poj1149的更多相关文章

  1. POJ1149 PIGS

    想了好久啊...(#-.-) 开始想到m*n个点的构图,明显超时,于是考虑压缩节点个数 我们发现每个猪圈最后被有且只有一个人调整,于是想到对于一个人,连接他能调整的每个猪圈的上一个控制人.(不懂可以开 ...

  2. POJ-1149 PIGS---最大流+建图

    题目链接: https://vjudge.net/problem/POJ-1149 题目大意: M个猪圈,N个顾客,每个顾客有一些的猪圈的钥匙,只能购买这些有钥匙的猪圈里的猪,而且要买一定数量的猪,每 ...

  3. [BZOJ1280][POJ1149]Emmy卖猪pigs

    [BZOJ1280][POJ1149]Emmy卖猪pigs 试题描述 Emmy在一个养猪场工作.这个养猪场有 \(M\) 个锁着的猪圈,但Emmy并没有钥匙.顾客会到养猪场来买猪,一个接着一个.每一位 ...

  4. POJ1149 PIGS 【最大流 + 构图】

    题目链接:http://poj.org/problem?id=1149 PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...

  5. POJ1149 PIGS [最大流 建图]

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20662   Accepted: 9435 Description ...

  6. POJ1149 PIGS (网络流)

                                                                             PIGS Time Limit: 1000MS   M ...

  7. POJ1149 PIGS 【最大流量】

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16555   Accepted: 7416 Description ...

  8. poj1149构图题

     引题解: 这道题目的大意是这样的:⦁ 有 M 个猪圈(M ≤ 1000),每个猪圈里初始时有若干头猪.⦁ 一开始所有猪圈都是关闭的.⦁ 依次来了 N 个顾客(N ≤ 100),每个顾客分别会打开指定 ...

  9. POJ1149(最大流)

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21678   Accepted: 9911 Description ...

随机推荐

  1. Yii框架中集成phprpc、hprose

    在项目开发的过程中有时候会涉及到对外提供接口供第三方程序调用或者是不同程序间需要相互通信,那么最通用的做法是用传统的SOAP方式来实现,用XML的文档格式来作为传输载体.但是这种方式不灵活,支持的数据 ...

  2. 怎样下载安装Firebug和使用Firebug

    Firebug是基于火狐(FireFox)浏览器的一个插件,它的作用是给Web页面开发者一个很好的测试前端页面代码的工具.所以深受网页开发者或网页布局爱好者的喜爱.像我们用DIV+CSS和html所写 ...

  3. C++ list 类学习笔记

    双向循环链表list list是双向循环链表,,每一个元素都知道前面一个元素和后面一个元素.在STL中,list和vector一样,是两个常被使用的容器.和vector不一样的是,list不支持对元素 ...

  4. 分享自lordinloft 《[转载]COMPILE_OPT 的用法介绍》

    来源:http://blog.sina.com.cn/s/blog_63180b75010117oj.html#bsh-73-372143085

  5. 五、mysql存储引擎

    show variable like 'table_type'; 显示系统默认存储引擎 show engine\G 显示系统支持存储殷勤 =============================== ...

  6. css3 简单界面动画

    asdasdasdasda asdasdasdasda

  7. 总有你需要的之 ios 小技巧 (下)

    图片上绘制文字 写一个UIImage的category NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultPara ...

  8. C# 5.0 TAP 模式下的HTTP Get和Post

    标题有点瘆人,换了工作之后很少写代码了,之前由于签了保密协议,不敢把代码拿出来分享给大家,只能摘抄网上的, 今斗胆拿出来晒晒,跪求指点,直接上代码吧 public class HTTPHelper : ...

  9. js检测对象的类型

    在JavaScript中,想要判断某个对象值属于哪种内置类型,最靠谱的做法就是通过Object.prototype.toString方法. 示例: var array=[1,2,3]; Object. ...

  10. Log4net Level

    ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); l ...