PIGS

Time Limit: 1000ms
Memory Limit: 10000KB

This problem will be judged on PKU.
64-bit integer(整数) IO format: %lld      Java class name: Main

 
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 arives, 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
  
  建模题,这里需要注意对空间的优化。
  题意:迈克有个养猪场,养猪场里有M个猪圈,每个猪圈都上了锁。迈克没有钥匙,而要买猪的顾客一个接一个来到养猪场,每个顾客有一些猪圈的钥匙,要买一定数量的猪。当每个顾客来时,将有钥匙的猪圈全部打开,从中挑出一些买走,然后迈克可以重新分配这些猪圈里面的猪。当顾客离开后,门又被锁上。问迈克最多可以卖多少猪。
  建模:先从源点给每个猪圈连一条边,容量是猪圈中猪的头数。这时再添加顾客,对于每一个顾客,查找他要开的每一个猪圈,如果他要开猪圈A,那么现在分情况讨论:
  <1>若以前(先后顺序,时间上的)没有顾客开过A猪圈,那么就连一条A到这个顾客的边,容量为INF,同时标记这个人为这个猪圈的“开启者”
  <2>若有,则将A的“开启者”连到这个人,容量为INF
  最后每个顾客连边到汇点,容量为各自的需求,接着跑一遍最大流就可以啦,这里我用了ISAP算法
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue> using namespace std;
const int INF=;
const int maxn=,maxm=;
int cnt,fir[maxn],nxt[maxm],cap[maxm],to[maxm],dis[maxn],gap[maxn],path[maxn],used[maxn]; void addedge(int a,int b,int c)
{
nxt[++cnt]=fir[a];
to[cnt]=b;
cap[cnt]=c;
fir[a]=cnt;
} bool BFS(int S,int T)
{
memset(dis,,sizeof(dis));
dis[T]=;
queue<int>q;q.push(T);
while(!q.empty())
{
int node=q.front();q.pop();
for(int i=fir[node];i;i=nxt[i])
{
if(dis[to[i]])continue;
dis[to[i]]=dis[node]+;
q.push(to[i]);
}
}
return dis[S];
}
int fron[maxn];
int ISAP(int S,int T)
{
if(!BFS(S,T))
return ;
for(int i=;i<=T;i++)++gap[dis[i]];
int p=S,ret=;
memcpy(fron,fir,sizeof(fir));
while(dis[S]<=T)
{
if(p==T){
int f=INF;
while(p!=S){
f=min(f,cap[path[p]]);
p=to[path[p]^];
}
p=T;ret+=f;
while(p!=S){
cap[path[p]]-=f;
cap[path[p]^]+=f;
p=to[path[p]^];
}
}
int &ii=fron[p];
for(;ii;ii=nxt[ii]){
if(!cap[ii]||dis[to[ii]]+!=dis[p])
continue;
else
break;
}
if(ii){
p=to[ii];
path[p]=ii;
}
else{
if(--gap[dis[p]]==)break;
int minn=T+;
for(int i=fir[p];i;i=nxt[i])
if(cap[i])
minn=min(minn,dis[to[i]]);
gap[dis[p]=minn+]++;
fron[p]=fir[p];
if(p!=S)
p=to[path[p]^];
}
}
return ret;
} void Init()
{
memset(fir,,sizeof(fir));
memset(used,,sizeof(used));
cnt=;
}
int main()
{
int n,m,num,k,need;
while(~scanf("%d%d",&m,&n))
{
Init();
for(int i=;i<=m;i++){
scanf("%d",&num);
addedge(,i,num);
addedge(i,,);
}
for(int i=m+;i<=m+n;i++){
scanf("%d",&k);
while(k--){
scanf("%d",&num);
if(used[num]){
addedge(used[num],i,INF);
addedge(i,used[num],);
}
else{
used[num]=i;
addedge(num,i,INF);
addedge(i,num,);
} }
scanf("%d",&need);
addedge(i,n+m+,need);
addedge(n+m+,i,);
}
printf("%d\n",ISAP(,n+m+));
}
return ;
}

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

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

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

  2. POJ 1149 PIGS(Dinic最大流)

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

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

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

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

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

  5. POJ 1149 PIGS(最大流)

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

  6. POJ 1149 PIGS 建图,最大流

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

  7. POJ 1149 PIGS (AC这道题很不容易啊)网络流

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

  8. POJ 1149 PIGS 【网络流】

    题意: m n   //有m个猪圈,n个人卖猪. a1...am    //编号为i的猪圈里有ai头猪. b1 c1...cb1 d1   //第i个人有bi把钥匙,分别是ci猪圈的,其它猪圈里的猪都 ...

  9. POJ 1149 PIGS ★(经典网络流构图)

    [题意] 有M个猪圈,每个猪圈里初始时有若干头猪.一开始所有猪圈都是关闭的.依 次来了N个顾客,每个顾客分别会打开指定的几个猪圈,从中买若干头猪.每 个顾客分别都有他能够买的数量的上限.每个顾客走后, ...

随机推荐

  1. Ubuntu16.04LTS安装

    1. 制作u盘启动盘 下载ubuntu-16.04-desktop-amd64.iso文件后,使用u盘启动盘制作工具:Win32DiskImager(14.04LTS后都需要用到这工具制作:https ...

  2. table转list

    DataTable数据集转换为List非泛型以及泛型方式 前言 DataTable是断开式的数据集合,所以一旦从数据库获取,就会在内存中创建一个数据的副本,以便使用.由于在实际项目中,经常会将 Dat ...

  3. ASP.NET Boilerplate Castle容器无缝添加日志功能

    以添加log4net日志框架为例进行讲解 1.通常log4net的配置参数放在单独的配置文件中,但也可以写在web.config中,这里在我们的web项目中添加log4net.config应用配置文件 ...

  4. 国人编写的开源 .net Ioc 框架——My.Ioc 简介

    My.Ioc 是作者开发的一款开源 IoC/DI 框架,下载地址在此处.它具有下面一些特点: 高效 在实现手段上,My.Ioc 通过使用泛型.缓存.动态生成代码.延迟注册.尽量使用抽象类而非接口等方式 ...

  5. 手势交互之GestureDetector

    GsetureDetector 一.交互过程 触屏的一刹那,触发MotionEvent事件 被OnTouchListener监听,在onTouch()中获得MotionEvent对象 GestureD ...

  6. Orace数据库锁表的处理与总结<摘抄与总结三>

    当Oracle数据库发生TX锁等待时,如果不及时处理常常会引起Oracle数据库挂起,或导致死锁的发生,产生ORA-60的错误. TX锁等待的分析 Oracle数据库中一般使用行级锁. 当Oracle ...

  7. 为Angular-UEditor增加工具栏属性

    感谢胡大大分享的的开源项目 Angular 的 UEditor 插件 Angular-UEditor 本文只是修改了angular-ueditor.js,加入了对工具栏的定制,方便项目使用 1 (fu ...

  8. 『重构--改善既有代码的设计』读书笔记----Replace Data Value with Object

    当你在一个类中使用字段的时候,发现这个字段必须要和其他数据或者行为一起使用才有意义.你就应该考虑把这个数据项改成对象.在开发初期,我们对于新类中的字段往往会采取简单的基本类型形式来保存,但随着我们开发 ...

  9. ThinkPHP 笔记

    1.循环中使用比较运算符 <volist name="subjects" id="v">       <option value=" ...

  10. [walkthrough] 在Asp.net MVC6 RC里使用NLog,并且把配置集成到config.json

    说明一下:本文基于随visual studio 2015 RC公开的DNX1.0.0-beta4,git上最新的aspnet的开发版本已经发生了很大变化. 首先,理论部分看[汤姆大叔的博客] 解读AS ...