Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u

Submit
Status

Description

Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come to the farm one after another. Each of them has keys to some pig-houses and wants
to buy a certain number of pigs.

All data concerning customers planning to visit the farm on that particular day are available to Mirko early in the morning so that he can make a sales-plan in order to maximize the number of pigs sold.


More precisely, the procedure is as following: the customer arrives, opens all pig-houses to which he has the key, Mirko sells a certain number of pigs from all the unlocked pig-houses to him, and, if Mirko wants, he can redistribute the remaining pigs across
the unlocked pig-houses.

An unlimited number of pigs can be placed in every pig-house.

Write a program that will find the maximum number of pigs that he can sell on that day.

Input

The first line of input contains two integers M and N, 1 <= M <= 1000, 1 <= N <= 100, number of pighouses and number of customers. Pig houses are numbered from 1 to M and customers are numbered from 1 to N.


The next line contains M integeres, for each pig-house initial number of pigs. The number of pigs in each pig-house is greater or equal to 0 and less or equal to 1000.


The next N lines contains records about the customers in the following form ( record about the i-th customer is written in the (i+2)-th line):


A K1 K2 ... KA B It means that this customer has key to the pig-houses marked with the numbers K1, K2, ..., KA (sorted nondecreasingly ) and that he wants to buy B pigs. Numbers A and B can be equal to 0.

Output

The first and only line of the output should contain the number of sold pigs.

Sample Input

3 3
3 1 10
2 1 2 2
2 1 3 3
1 2 6

Sample Output

7

Source

Croatia OI 2002 Final Exam - First day



最大流经典建图,有m个猪圈,n个顾客,m个猪圈的钥匙在n个顾客手中,n个顾客一次来卖猪,当他们打开过猪圈之后农场主可以将这些猪圈里的猪按照自己的意愿进行转移,求这n个顾客可以买到多少头猪



建立超级源点与超级汇点,超级源点指向每个猪圈到达的第一个顾客,随后的顾客与前一个顾客产生一条正无穷的边权,而从源点出来的边权就是该猪圈的猪的数目,因为猪圈打开过之后猪就可以转移,那么随后的边权就应该是无穷,因为之后这个猪圈中猪的数目是不确定的,但是超级汇点要与每一个顾客建边,边权就是该顾客的需求量



可以参照大牛的分析,非常详细

http://wenku.baidu.com/view/0ad00abec77da26925c5b01c.html

#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
#define MAXN 2000
#define MAXM 50000
#define INF 10000000
struct node
{
int u,v,cap,flow,next;
}edge[MAXM];
int cur[MAXN],dis[MAXN],vis[MAXN];
int head[MAXN],pig[MAXN],want[MAXN];
vector<int>first[MAXN];
int m,n,top;
void init()
{
top=0;
memset(head,-1,sizeof(head));
for(int i=0;i<=m;i++)
first[i].clear();
}
void add(int u,int v,int w)
{
int i=-1;
for(i=head[u];i!=-1;i=edge[i].next)//判断是否有重边
{
if(edge[i].v==v)
break;
}
if(i==-1)//没有重边的话直接建图
{
node E1={u,v,w,0,head[u]};
edge[top]=E1;
head[u]=top++;
node E2={v,u,0,0,head[v]};
edge[top]=E2;
head[v]=top++;
}
else//重边就流量汇总
{
if(w!=INF) edge[i].cap+=w;
}
}
void getmap()
{
for(int i=1;i<=m;i++)
{
int u=first[i][0];
add(0,u,pig[i]);//超级源点与每个猪圈进行联系,边权为猪圈猪的数目
for(int j=0;j<first[i].size()-1;j++)
{
int x=first[i][j];//将第一个顾客与后来的顾客产生联系
int y=first[i][j+1];
add(x,y,INF);
}
}
for(int i=1;i<=n;i++)
add(i,n+1,want[i]);//超级汇点与每一个顾客链接
}
void input()
{
int t,a;
for(int i=1;i<=m;i++)
scanf("%d",&pig[i]);
for(int i=1;i<=n;i++)
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&a);
first[a].push_back(i);//first[a][0]就是第一个顾客
}
scanf("%d",&want[i]);
}
}
bool bfs(int s,int e)
{
memset(vis,0,sizeof(vis));
memset(dis,-1,sizeof(dis));
queue<int>q;
while(!q.empty()) q.pop();
q.push(s);
vis[s]=1;
dis[s]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(!vis[E.v]&&E.cap>E.flow)
{
vis[E.v]=1;
dis[E.v]=dis[E.u]+1;
if(E.v==e) return true;
q.push(E.v);
}
}
}
return false;
}
int dfs(int x,int a,int e)
{
if(a==0||x==e)
return a;
int flow=0,f;
for(int i=cur[x];i!=-1;i=edge[i].next)
{
node& E=edge[i];
if(dis[x]+1==dis[E.v]&&(f=dfs(E.v,min(a,E.cap-E.flow),e))>0)
{
E.flow+=f;
edge[i^1].flow-=f;
flow+=f;
a-=f;
if(a==0) break;
}
}
return flow;
}
int MAXflow(int s,int e)
{
int flow=0;
while(bfs(s,e))//是否可以增广
{
memcpy(cur,head,sizeof(head));
flow+=dfs(s,INF,e); //printf("%d\n",flow);
}
return flow;
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
init();
input();
getmap();
printf("%d\n",MAXflow(0,n+1));
}
return 0;
}

poj--1149--PIGS(最大流经典建图)的更多相关文章

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

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

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

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

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

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

  4. POJ1149 最大流经典建图PIG

    题意:       有一个人,他有m个猪圈,每个猪圈里都有一定数量的猪,但是他没有钥匙,然后依次来了n个顾客,每个顾客都有一些钥匙,还有他要卖猪的数量,每个顾客来的时候主人用顾客的钥匙打开相应的门,可 ...

  5. poj 1149 pigs ---- 最大流

    题意以及分析:http://ycool.com/post/zhhrrm6#rule3 主要是建图,简化图,然后在套最大流的模板. #include <iostream> #include& ...

  6. POJ 2226 最小点覆盖(经典建图)

    Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8881   Accepted: 3300 Desc ...

  7. poj 1149 pigs(最大流)

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

  8. hdoj--5093--Battle ships(二分图经典建图)

    Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

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

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

随机推荐

  1. Linux Shell脚本编程学习笔记和实战

    http://www.1987.name/141.html shell基础 终端打印.算术运算.经常使用变量 Linux下搜索指定文件夹下特定字符串并高亮显示匹配关键词 从键盘或文件里获取标准输入 [ ...

  2. Android核心服务解析篇(三)——Android系统的启动

    从大的方面来说.Android系统的启动能够分为两个部分:第一部分是Linux核心的启动,第二部分是Android系统的启动. 第一部分主要包含系统引导,核心和驱动程序等,因为它们不属于本篇要讲的内容 ...

  3. hadoop1.1.0的伪分布搭建步骤

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWFuYnVyZW4wMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  4. BNUOJ34980方(芳)格(哥)取数(好坑)

    方(芳)格(哥)取数 Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class n ...

  5. 纯CSS实现小圆点和三角形图案

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  6. zzulioj--1775-- 和尚特烦恼1——是不是素数(素数水题)

    1775: 和尚特烦恼1--是不是素数 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 563  Solved: 193 SubmitStatusWeb ...

  7. hdoj--2036--改革春风吹满地(数学几何)

    改革春风吹满地 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  8. POJ 3190 priority_queue 贪心

    思路: 贪心?就算是吧 先把所有的开始时间排个序 如果当前的能匹配上已有的牛栏,就找开始时间最早的那个. 否则新加一个牛栏 整个过程用priority_queue实现就OK了.. //By Siriu ...

  9. miniUI-SelectGrid 弹出选择表格-翻页选中

    介绍 mini中已经给出 弹出表格的里例子 :MiniUi版本 但是在应用过程中遇到写小问题就是没有办法翻页后一并连之前翻页选中的一起提交 以下是解决方案 正文 下面首先介绍  JS 代码 //存储已 ...

  10. CUDA笔记13

    在新的环境上用CUTIL的时候,出现了问题.无法解析的外部符号 __imp_cutCheckCmdLineFlag 问题描述: kernel.cu.obj : error LNK2019: 无法解析的 ...