POJ1149 PIGS (网络流)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 20146 | Accepted: 9218 |
Description
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
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
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 (网络流)的更多相关文章
- 【poj1149】 pigs 网络流最大流问题
描述 Description 尼克在一家养猪场工作,这家养猪场共有M间锁起来的猪舍,由于猪舍的钥匙都给了客户,所以尼克没有办法打开这些猪舍,客户们从早上开始一个接一个来购买生猪,他们到达后首先用手中的 ...
- poj 1149 Pigs 网络流-最大流 建图的题目(明天更新)-已更新
题目大意:是有M个猪圈,N个顾客,顾客要买猪,神奇的是顾客有一些猪圈的钥匙而主人MIRKO却没有钥匙,多么神奇?顾客可以在打开的猪圈购买任意数量的猪,只要猪圈里有足够数量的猪.而且当顾客打开猪圈后mi ...
- poj1149 PIGS 最大流(神奇的建图)
一开始不看题解,建图出错了.后来发现是题目理解错了. if Mirko wants, he can redistribute the remaining pigs across the unlock ...
- POJ1149 PIGS 【最大流 + 构图】
题目链接:http://poj.org/problem?id=1149 PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- POJ 1149:PIGS 网络流经典题
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18345 Accepted: 8354 Description ...
- POJ1149 PIGS [最大流 建图]
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20662 Accepted: 9435 Description ...
- POJ1149 PIGS
想了好久啊...(#-.-) 开始想到m*n个点的构图,明显超时,于是考虑压缩节点个数 我们发现每个猪圈最后被有且只有一个人调整,于是想到对于一个人,连接他能调整的每个猪圈的上一个控制人.(不懂可以开 ...
- POJ1149 PIGS 【最大流量】
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16555 Accepted: 7416 Description ...
- BZOJ1280 Emmy卖猪pigs 网络流
正解:网络流 解题报告: 传送门! 我网络流的基础题都还麻油做完就来做这个了,,,wsl,,, 首先想下最基础的构图方法 不难想到把猪圈和顾客分别当做节点,然后新建一个源点和汇点 然后考虑怎么连边,首 ...
随机推荐
- SSH(2)-- ssh_config和sshd_config
假定服务器ip为192.168.1.139,ssh服务的端口号为22,服务器上有个用户为pi,两边都是ubuntu. ssh_config和sshd_config都是ssh服务器的配置文件,二者区别在 ...
- Cygwin下载,安装教程
Cygwin是一个用于在Windows上模拟Linux环境的软件,由于工作上的需要,我要使用它,至于为什么用它,我在这里不做过多的解释,本文的目的,旨在于解决Cygwin安装上的问题. 原始的安装Cy ...
- 修改Azure Website 移动服务 默认时区
Azure Website 默认时区为国际标准时间,对中国用户来说不太方便友好,如何设置成北京时间呢? 打开Azure Website的“配置”页,找到“应用设置”节点. 在应用设置中添加设置项,密钥 ...
- 对于C#中的一些点滴你真的理解了吗?
废话不多说看题目,看看我们自己真的理解了吗? 1.如下代码输出的结果是什么? public class A{ public virtual void Func(int number=10) { Co ...
- 在iis6.0公布asp.net mvc3网站
在iis6.0发布asp.net mvc3网站 这个问题一直困扰了我很长一段时间,终于在今天综合网上查的资料把它解决了. 在iis6.0发布asp.net mvc3网站 1 需要在服务器下安装.n ...
- arguments.callee
arguments.callee在哪个函数中运行,他就代表哪个函数,一般在匿名函数中.在匿名函数中有时需要自己调用自己,但是由于是匿名函数,没有名字,所以可以用arguments.callee来代替匿 ...
- vc设置按钮文字颜色
设置按钮文字颜色使用 CMFCBUTTON即可 在OnInitDialog函数加入如下内容即可 ((CMFCButton*)GetDlgItem(IDC_MFCBUTTON1))->SetTex ...
- PHP图像处理之在原图像处理
处理原有的图像 图片处理,缩放,裁剪,翻转,旋转,透明,锐化等图片操作 一.创建图片资源 imagecreatetruecolor(width,hei ...
- 文本编辑器vim
Vim是一个功能强大的全屏幕文本编辑器,是Linux/UNIX上最大的文本编辑器,它的作用是建立.编辑.显示文本文件. Vim没有菜单,只有命令.
- HDU 1848
http://acm.hdu.edu.cn/showproblem.php?pid=1848 利用计算grundy数组,把一类博弈转化为nim博弈,最后x不为0为先手必胜态 #include < ...