PIGS
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20421   Accepted: 9320

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

题目连接:http://poj.org/problem?id=1149

题意:有m个猪圈,n个顾客。下一行输入m个猪圈里面猪的数量。接下来n行,每一行输入a,k1,k2,……,ka,b。表示这个顾客拥有ki猪圈的钥匙,他最多买的猪的数量b。开始时,猪圈是关着的,顾客来购买时,打开所有顾客自己能打开的猪圈,Mirko从中选一些猪卖给顾客,Mirko可以重新分配被打开的猪圈里面的猪。顾客离开后,猪圈关闭。问Mirko最多卖掉多少头猪。

思路:注意因为可以重新分配猪圈里面的猪,网络流的最大流。设置一个源点0和一个汇点n+1,顾客是其他节点。顾客j紧跟在顾客i之后打开某个猪圈,则边<i,j>的容量是+∞。如果某个猪圈是顾客j第一个打开则边<0,j>的容量是这个猪圈的数量。边<j,n+1>的容量是b。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN=1e2+,INF=1e7+;
struct Edge
{
int c,f,g;
} edge[MAXN][MAXN];
int path[MAXN],flow[MAXN];
int sum[];
int sign[];
queue<int>q;
int bfs(int Start,int End)
{
int u,v;
memset(path,-,sizeof(path));
memset(flow,,sizeof(flow));
while(!q.empty()) q.pop();
path[Start]=;
flow[Start]=INF;
q.push(Start);
while(!q.empty())
{
u=q.front();
q.pop();
if(u==End) break;
for(v=Start; v<=End; v++)
{
if(v!=Start&&path[v]==-&&edge[u][v].g)
{
path[v]=u;
flow[v]=flow[u]<edge[u][v].g?flow[u]:edge[u][v].g;
q.push(v);
}
}
}
return flow[End];
}
int ford(int Start,int End)
{
int max_flow=,pre,now,step;
while((step=bfs(Start,End))!=)
{
max_flow+=step;
now=End;
while(now!=Start)
{
pre=path[now];
edge[pre][now].g-=step;
edge[now][pre].g+=step;
now=pre;
}
}
return max_flow;
}
int main()
{
int i,m,n;
while(~scanf("%d%d",&m,&n))
{
for(i=; i<=m; i++) scanf("%d",&sum[i]);
for(i=; i<=n; i++)
{
int a,k,b;
scanf("%d",&a);
while(a--)
{
scanf("%d",&k);
if(sign[k]==)
edge[sign[k]][i].c+=sum[k];
else edge[sign[k]][i].c=INF;
edge[sign[k]][i].g=edge[sign[k]][i].c;
sign[k]=i;
}
scanf("%d",&b);
edge[i][n+].g=edge[i][n+].c=b;
}
int Start=,End=n+;
printf("%d\n",ford(Start,End));
}
return ;
}

最大流

												

POJ 1149PIGS 网络流 最大流的更多相关文章

  1. POJ 1273 网络流(最大流)模板

    http://poj.org/problem?id=1273 这道题很值得反思,弄了一下午,交上去先是一直编译错误,而在本地运行没有问题, 原因可能是oj的编译器版本老旧不支持这样的写法 G[from ...

  2. POJ 1459-Power Network(网络流-最大流-ISAP)C++

    Power Network 时间限制: 1 Sec  内存限制: 128 MB 题目描述 A power network consists of nodes (power stations, cons ...

  3. [POJ1273][USACO4.2]Drainage Ditches (网络流最大流)

    题意 网络流最大流模板 思路 EK也不会超时 所以说是一个数据比较水的模板题 但是POJ有点坑,多组数据,而且题目没给 哭得我AC率直掉 代码 用的朴素Dinic #include<cstdio ...

  4. HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)

    HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...

  5. HDU1532 网络流最大流【EK算法】(模板题)

    <题目链接> 题目大意: 一个农夫他家的农田每次下雨都会被淹,所以这个农夫就修建了排水系统,还聪明的给每个排水管道设置了最大流量:首先输入两个数n,m ;n为排水管道的数量,m为节点的数量 ...

  6. Redraw Beautiful Drawings(hdu4888)网络流+最大流

    Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...

  7. A simple Gaussian elimination problem.(hdu4975)网络流+最大流

    A simple Gaussian elimination problem. Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...

  8. 【bzoj3130】[Sdoi2013]费用流 二分+网络流最大流

    题目描述 Alice和Bob做游戏,给出一张有向图表示运输网络,Alice先给Bob一种最大流方案,然后Bob在所有边上分配总和等于P的非负费用.Alice希望总费用尽量小,而Bob希望总费用尽量大. ...

  9. 【bzoj1822】[JSOI2010]Frozen Nova 冷冻波 计算几何+二分+网络流最大流

    题目描述 WJJ喜欢“魔兽争霸”这个游戏.在游戏中,巫妖是一种强大的英雄,它的技能Frozen Nova每次可以杀死一个小精灵.我们认为,巫妖和小精灵都可以看成是平面上的点. 当巫妖和小精灵之间的直线 ...

随机推荐

  1. js中js数组、对象与json之间的转换

    在数据传输过程中,json是以文本,即字符串的形式传递的,而JS操作的是JSON对象,所以,JSON对象和JSON字符串之间的相互转换是关键.例如:JSON字符串:var str1 = '{ &quo ...

  2. js一个抽奖的例子

    朋友公司开年会,帮忙写了个抽奖的demo,源码如下,github中有程序: html: <header> lottery demo </header> <div clas ...

  3. 思达index网站

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. C++模板类继承的一个小技巧

    先说一下background前段时间想实现一个Sqlite localstorage的功能,对应不同的Model 实体有不同的table, 每一次sql操作的函数签名中会有model实体中的struc ...

  5. 关于C语言里指针的基本概念

    C是很强大的一门语言,然而C语言的强大并不是强大在他的语法和“.h”文件,而是指针.    对指针通用的认知都是:指针是指向内存地址的一个变量.对于这句话,我是这么理解的:核心有两点,第一个是“指向内 ...

  6. asp.net 新项目遇到的坑

    1.新拿来的项目,能正常跑,但是想熟悉,运用断点调试,f11却发现出了这个问题 此提示:应该是缺失dll文件  2.于是重新生成项目出现,发现Log4Net,有文件,但是出现这个提示 思考:一版本不对 ...

  7. dubbo-admin重新编译后部署出现 valid LOC header (bad signature)异常

    前段时间从网上下载dubbo-admin发现在jdk1.7下不可用,后来根据网上说的升级了jar包版本还是有问题,于是观察了编译发现编译时出现类似如下异常: --------------------- ...

  8. 配置 Haproxy 防范 DDOS 攻击

    作为 load balancer, Happroxy 常常作为服务器的前端,向外界用户提供服务的入口,如果能在入口处处理安全相关问题,将极大简化后端的设计.事实上,Haproxy 不仅仅是一款开源出色 ...

  9. 丰富自己的代码库-SqlServerHelper(Ado)

    设计思路很简单,就是把数据封装为DataTable类,封装了类型转换,使用者可以不必考虑任何类型转换,而使用VC提供的CString即可.封装了按行号查找功能,先看代码 #pragma once #i ...

  10. delphi中midas是什么

    Delphi中MIDAS到底是什么呢?和他相关组件是什么呢?   MIDAS(Multitiered Distributed Application Services)多层分布式应用服务.   Del ...