题意以及分析:http://ycool.com/post/zhhrrm6#rule3

  主要是建图,简化图,然后在套最大流的模板。

#include <iostream>
#include<string.h>
#include<cstdio>
using namespace std; struct Node
{
int v,cap,flow,next; Node(){}
Node(int _v,int _cap,int _flow,int _next)
{
v=_v;
cap=_cap;
flow=_flow;
next=_next;
}
}; const int INF=;
Node edges[];
int pig[],a[][];
int n,m,s,t,head[],e; void Add(int u,int v,int cap)
{
edges[e]=Node(v,cap,,head[u]);
head[u]=e++;
edges[e]=Node(u,,,head[v]);
head[v]=e++;
} int num[],h[],curedge[],pre[]; int Maxflow(int s,int t,int n)
{
int ans=,i,k,x,d,u; memset(num,,sizeof(num));
memset(h,,sizeof(h));
for(i=;i<=n;i++) curedge[i]=head[i];
num[n]=n;u=s;
while(h[u]<n)
{
if(u==t)
{
d=INF+;
for(i=s;i!=t;i=edges[curedge[i]].v) if(d>edges[curedge[i]].cap)
k=i,d=edges[curedge[i]].cap;
for(i=s;i!=t;i=edges[curedge[i]].v)
{
x=curedge[i];
edges[x].cap-=d;
edges[x].flow+=d;
edges[x^].cap+=d;
edges[x^].flow-=d;
}
ans+=d;u=k;
}
for(i=curedge[u];i!=-;i=edges[i].next) if(edges[i].cap>&&h[u]==h[edges[i].v]+)
break;
if(i!=-)
{
curedge[u]=i;
pre[edges[i].v]=u;
u=edges[i].v;
}
else
{
if(--num[h[u]]==) break;
curedge[u]=head[u];
for(x=n,i=head[u];i!=-;i=edges[i].next) if(edges[i].cap>&&h[edges[i].v]<x)
x=h[edges[i].v];
h[u]=x+;num[h[u]]++;
if(u!=s) u=pre[u];
}
}
return ans;
} int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
int i,j,x,p,num; s=;t=n+;
memset(head,-,sizeof(head));
e=;
for(i=;i<=m;i++) scanf("%d",&pig[i]);
for(i=;i<=m;i++) a[i][]=;
for(i=;i<=n;i++)
{
scanf("%d",&x);
while(x--) scanf("%d",&p),a[p][++a[p][]]=i;
scanf("%d",&num);
Add(i,t,num);
}
for(i=;i<=m;i++) if(a[i][]) Add(s,a[i][],pig[i]);
for(i=;i<=m;i++) for(j=;j<a[i][];j++) Add(a[i][j],a[i][j+],INF);
printf("%d\n",Maxflow(s,t,t));
}
return ;
}

poj 1149 pigs ---- 最大流的更多相关文章

  1. POJ 1149 - PIGS - [最大流构图]

    Time Limit: 1000MS Memory Limit: 10000K Description Mirko works on a pig farm that consists of M loc ...

  2. poj 1149 pigs(最大流)

    题目大意:迈克在农场工作,农场有 m 个猪舍,每个猪舍有若干只猪,但是迈克不能打开任何一间猪舍.有 n 个顾客前来购买,每个顾客有最大的购买数量,每个顾客可以购买某些猪舍的猪,且顾客可以打开这些猪舍, ...

  3. [poj] 1149 PIGS || 最大流经典题目

    原题 题目大意 给你m个猪圈以及每个猪圈里原来有多少头猪,先后给你n个人,每个人能打开一些猪圈并且他们最多想买Ki头猪,在每一个人买完后能将打开的猪圈中的猪顺意分配在这次打开猪圈里,在下一个人来之前 ...

  4. POJ 1149 PIGS(Dinic最大流)

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20738   Accepted: 9481 Description ...

  5. poj 1149 Pigs 网络流-最大流 建图的题目(明天更新)-已更新

    题目大意:是有M个猪圈,N个顾客,顾客要买猪,神奇的是顾客有一些猪圈的钥匙而主人MIRKO却没有钥匙,多么神奇?顾客可以在打开的猪圈购买任意数量的猪,只要猪圈里有足够数量的猪.而且当顾客打开猪圈后mi ...

  6. 网络流(最大流):POJ 1149 PIGS

    PIGS Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. 64-bit integer(整数) ...

  7. POJ 1149 PIGS(最大流)

    Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock an ...

  8. poj 1149 PIGS【最大流经典建图】

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18727   Accepted: 8508 Description ...

  9. poj 1149 PIGS(最大流经典构图)

    题目描述:迈克在一个养猪场工作,养猪场里有M 个猪圈,每个猪圈都上了锁.由于迈克没有钥匙,所以他不能打开任何一个猪圈.要买猪的顾客一个接一个来到养猪场,每个顾客有一些猪圈的钥匙,而且他们要买一定数量的 ...

随机推荐

  1. 【STL】算法 — partial_sort

    partial_sort接受一个middle迭代器,使序列中的middle-first个最小元素以递增顺序排序.置于[first, middle)内.以下是測试代码: #include <ios ...

  2. JS 利用CNZZ进行站长统计

    利用CNZZ对网站进行站长统计 前端页面只需要在body中添加如下代码即可: <script type="text/javascript">var cnzz_proto ...

  3. Android应用开发基本流程及测试运行

    (1)Android App工程项目的创建 File—New—New Project 设置项目.程序包的名称 设置项目名称为MyDiary,程序包名为org.socrates.mydiary.acti ...

  4. error: undefined reference to `XXX::XXX(type1, ypte2)

    moc_fortunethread.cpp:100: error: undefined reference to `FortuneThread::GetToParentThread(QString, ...

  5. GridView专栏

    鉴于GridView的强大,鄙人突然心血来潮,想把GridView单独拿出来整理一下. (一)gridview如何加自增长列 protected void GridView1_RowDataBound ...

  6. 等待事件:enq: HW - contention和enq: TM - contention

    今天生成了生产库前几日的AWR报告,发现等待事件中出现了一个陌生的event--enq: HW - contention,google一下是ASSM(Auto Segment Space Manage ...

  7. C++命名准则

    总则:命名用英语,单词简单,明了.意义明确.过长的单词可以使用省略.一般是去掉元音字母形成省略.如果有通用的缩写,应该采用通用的缩写. 1.函数命名 1.1:全局函数:采用限定词+动词(+名词的原则) ...

  8. 存储、读取——Android应用程序内置的文件夹

    1.将数据存储到应用程序的文件夹,并读写 Context提供了两个方法,打开应用程序文件夹的I/O,若文件不存在则创建 FileInputStream openFileInputStream(Stri ...

  9. ODI Studio拓扑结构的创建与配置(Oracle)

    一.概念解释 Topology Manager主要用来管理下面5类任务,并将信息存储在主资料库中,供所有模块共享使用. 物理体系结构: 定义各种技术及其数据服务器.物理架构.物理代理.数据服务器瞎可以 ...

  10. 关于chrome浏览器更新后快捷工具失效的解决方法

    更新chrome浏览器到29.0时发现快捷工具的双击关闭标签页失效了,解决办法: 1.打开chrome的文件夹.C:\Program Files (x86)\Google\Chrome\Applica ...