非常好的网络流

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

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

对于每个猪圈,假设有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. linux 输入子系统(3)----事件处理(input_handler层)

    输入子系统主要设计input_dev.input_handler.input_handle.如下: [1]每个struct input_dev代表一个输入设备 struct input_dev { c ...

  2. linux 输入子系统(1)----系统概述

    输入设备的工作中,只是中断.读键值/坐标值是设备相关的,而输入事件的缓冲区管理以及字符设备驱动的file_operations接口则对输入设备是通用的,基于此,内核设计了input输入子系统,由核心层 ...

  3. jsp+oracle 排序分页+Pageutil类

    1.rownum和排序 Oracle中的rownum的是在取数据的时候产生的序号,所以想对指定排序的数据去指定的rowmun行数据就必须注意了. SQL> select rownum ,id,n ...

  4. 第二章 约束和排序数据(SQL基础)

    第二章 约束和排序数据 1. 在 emp 表中选择工资介于 1500 到 2500 的员工的信息:                注意:使用 between 下边界 and 上边界时,条件包括边界值: ...

  5. 编辑器&IDE中适合程序员的字体

    adobe的免费字体 source Code Pro

  6. EXTJS 4.2 资料 控件之checkboxgroup的用法(静态数据)

    1.页面 1.1点击‘横幅’,需要动态显示隐藏文本框 { xtype: 'fieldset', title: '指定附加图&横幅设置', collapsible: true, items: [ ...

  7. poi导出到excel步骤分析

    在没用过poi之前感觉poi是很高大上的样子, 项目中用了发现poi的代码重复性很高类似于jdbc的模板代码, 项目中如果大量使用最好封装起来; 总结一下归结为6步 1 打开或新创建一个工作薄(使用H ...

  8. drupal CMS

    http://drupalchina.cn/ https://www.drupal.org

  9. PAT-乙级-1027. 打印沙漏(20)

    1027. 打印沙漏(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求你写个程序把给定的符号打印成 ...

  10. uva 11039

    水题  排序 判符号 #include <cstdio> #include <cstring> #include <algorithm> using namespa ...