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,,, 首先想下最基础的构图方法 不难想到把猪圈和顾客分别当做节点,然后新建一个源点和汇点 然后考虑怎么连边,首 ...
随机推荐
- 如何在redhat下安装办公软件(openoffice)
在redhat的client版本中自带有办公软件libreoffice,而在server版的redhat中却没有自带的办公软件,那么,如何在redhat的server版下安装办公软件呢? 方法一:配置 ...
- AngularJS结构简介
AngularJS是MVC架构,M是C里面的属性-值,C是js的class,V是DOM 各个关键特性的结构如下图所示: http://my.oschina.net/tommyfok/blog/2970 ...
- CodeForces 540C Program D
Description You play a computer game. Your character stands on some level of a multilevel ice cave. ...
- [windows驱动]基本概念
https://msdn.microsoft.com/zh-cn/library/windows/hardware/ff554721 1.设备节点和设备堆栈 在windows中,设备通过即插即用设备树 ...
- 【python】list。列表
列表 list 特点:有序,支持不同类型的元素在一个列表中,可变(使用sort方法排序,影响到的是列表自身而不是创建新的列表——这与字符串不同,所以说字符串是不可变的) 在python中列表也是对象, ...
- poj1159 dp最长公共子串
//Accepted 204 KB 891 ms //dp最长公共子串 //dp[i][j]=max(dp[i-1][j],dp[i][j-1]) //dp[i][j]=max(dp[i][j],dp ...
- TCP同步传送数据示例以及可能出现问题分析
TCP传送数据可以分为同步传送和异步传送,首先这里使用了TCP的同步传送方式,学习了TCP同步传送数据的原理. 同步工作方式是指利用TCP编写的程序执行到监听或者接受数据语句的时候,在未完成当前工作( ...
- CPU MPU MCU SOC SOPC关系及区别
在嵌入式开过程,会经常接触到一些缩写术语概念,这些概念在嵌入式行业中使用率非常高,下面我们就解释一下这些概念之间的关系和区别: 1.CPU(Central Processing Unit),是一台计算 ...
- Android布局文件layout.xml的一些属性值
第一类:属性值 true或者 false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 andr ...
- 步步入佳境---UI入门(3) --单视图控制器
视图控制器特点//1,抽象 视觉上没有效果//2,负责控制视图的显示方式//3,负责通知视图的显示内容//4,ios平台赋予的,收到内存警告和检测设备旋转@interface CHViewContr ...