PIGS
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20146   Accepted: 9218

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
【分析】直接套的书上的标号法模板。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 1000000007
typedef long long ll;
using namespace std;
const int N=;
const int M=;
int s,t,n,m,num,k;
int customer[N][N];
int flow[N][N];
int house[M],last[M];
int pre[N],minflow[N];
void Build() {
memset(last,,sizeof(last));
memset(customer,,sizeof(customer));
scanf("%d%d",&m,&n);
s=;
t=n+;
for(int i=; i<=m; i++)scanf("%d",&house[i]);
for(int i=; i<=n; i++) {
scanf("%d",&num);
for(int j=; j<num; j++) {
scanf("%d",&k);
if(last[k]==)customer[s][i]+=house[k];
else customer[last[k]][i]=inf;
last[k]=i;
}
scanf("%d",&customer[i][t]);
}
} void BFS() {
queue<int>q;
int p=;
memset(flow,,sizeof(flow));
minflow[]=inf;
while() {
while(!q.empty())q.pop();
for(int i=; i<N; i++)pre[i]=-;
pre[]=-;
q.push();
while(!q.empty()&&pre[t]==-) {
int v=q.front();
q.pop();
for(int i=; i<t+; i++) {
if(pre[i]==-&&(p=customer[v][i]-flow[v][i])) {
pre[i]=v;
q.push(i);
minflow[i]=min(p,minflow[v]);
}
}
}
if(pre[t]==-)break;
int j;
for(int i=pre[t],j=t; i>=; j=i,i=pre[i]) {
flow[i][j]+=minflow[t];
flow[j][i]=-flow[i][j];
}
}
for(int i=;i<t;i++)p+=flow[i][t];
printf("%d\n",p);
}
int main() {
Build();
BFS();
return ;
}

POJ1149 PIGS (网络流)的更多相关文章

  1. 【poj1149】 pigs 网络流最大流问题

    描述 Description 尼克在一家养猪场工作,这家养猪场共有M间锁起来的猪舍,由于猪舍的钥匙都给了客户,所以尼克没有办法打开这些猪舍,客户们从早上开始一个接一个来购买生猪,他们到达后首先用手中的 ...

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

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

  3. poj1149 PIGS 最大流(神奇的建图)

    一开始不看题解,建图出错了.后来发现是题目理解错了.  if Mirko wants, he can redistribute the remaining pigs across the unlock ...

  4. POJ1149 PIGS 【最大流 + 构图】

    题目链接:http://poj.org/problem?id=1149 PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...

  5. POJ 1149:PIGS 网络流经典题

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18345   Accepted: 8354 Description ...

  6. POJ1149 PIGS [最大流 建图]

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20662   Accepted: 9435 Description ...

  7. POJ1149 PIGS

    想了好久啊...(#-.-) 开始想到m*n个点的构图,明显超时,于是考虑压缩节点个数 我们发现每个猪圈最后被有且只有一个人调整,于是想到对于一个人,连接他能调整的每个猪圈的上一个控制人.(不懂可以开 ...

  8. POJ1149 PIGS 【最大流量】

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16555   Accepted: 7416 Description ...

  9. BZOJ1280 Emmy卖猪pigs 网络流

    正解:网络流 解题报告: 传送门! 我网络流的基础题都还麻油做完就来做这个了,,,wsl,,, 首先想下最基础的构图方法 不难想到把猪圈和顾客分别当做节点,然后新建一个源点和汇点 然后考虑怎么连边,首 ...

随机推荐

  1. 【C语言学习】-01 C基础

    本文目录: 0.进制转换 1.C数据类型 2.常量变量 3.运算符 4.表达式 5.格式化输入输出 回到顶部 0.进制转换 在计算机中存储的数据,主要是以二进制形式存在,而我们生活中主要使用的有十进制 ...

  2. Problem A CodeForces 560A

    Description A magic island Geraldion, where Gerald lives, has its own currency system. It uses bankn ...

  3. Keil 4.0 生成bin文件

    在菜单:Flash /configuare falsh tools ... option for Tatget '项目' /User中:在Run User Programs After Build/R ...

  4. MJPhotoBrowser 两个bug:回到小图模式时会闪动&大图太靠近底部

    最近项目需要写网络的相片视频浏览的库, 没时间重写,使用了MJPhotoBrowser,里面的一些bug 和解决写在下面 1.-[MJPhotoLoadingView setProgress:]: m ...

  5. Java面向对象的三大特征

    Java面向对象的三大特征 java面向对象的三大特征:“封装.继承.多态”.更多Java技术知识,请登陆疯狂软件教育官网.微信搜索微信号:疯狂软件,参加2015年优惠活动,有机会获得优惠劵和代金劵. ...

  6. 自定义UIPageControl

    iphone的UIPageControl控件可以显示用户huan'dong滑动到的页码.但是里面的小点的颜色时默认的白色.如果背景也是白色的hu话,你就悲剧了.于是乎上网找了一些资料,找到了改变UIP ...

  7. BZOJ 1912 巡逻

    重赋边权. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...

  8. iOS常用设计模式:MVC、单例、代理、观察者。

    MVC 模型-视图-控制器(MVC)设计模式 MVC根据角色划分类,涉及到三个角色: Model:模型保存应用程序的数据. View:视图是模型的可视化表示以及用户交互的控件. Controller: ...

  9. HDU 3047

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 和hdu 3038以及poj那个食物链一样,都是带权并查集,此题做法和hdu 3038完全相同,具体操作看 ...

  10. HDU 1082

    http://acm.hdu.edu.cn/showproblem.php?pid=1082 这题开始想复杂了,error并不包括表达式本身不合法的情况 我的方法是遇到右括号就开始处理栈,如果开始最外 ...