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,,, 首先想下最基础的构图方法 不难想到把猪圈和顾客分别当做节点,然后新建一个源点和汇点 然后考虑怎么连边,首 ...
随机推荐
- [vijos P1531] 食物链
做出的第一道NOI题目?(噗,还是看题解才会的…按某篇解题说的,这题就比我年轻四岁…T T 做的第一道IOI题目是USACO上的Packing Rectangles...这题比我还老!)对我等弱渣来说 ...
- DotNetBar v12.7.0.2 Fully Cracked
更新信息: http://www.devcomponents.com/customeronly/releasenotes.asp?p=dnbwf&v=12.7.0.2 如果遇到破解问题可以与我 ...
- [安卓]softap
http://www.cnblogs.com/javawebsoa/archive/2013/05/29/3106758.html
- python版恶俗古风自动生成器.py
python版恶俗古风自动生成器.py """ python版恶俗古风自动生成器.py 模仿自: http://www.jianshu.com/p/f893291674c ...
- compare:(字符串的大小比较)
1.字符串的比较是按照ascall码进行比较的 比如A比a的值小, if([string1 compare:string2] == (以下描述)) 如果比较的结果是NSOrderedDescendin ...
- 删除目录下所有gif的图片
find -name "*.gif" -exec rm -fv {} \;
- C#_控件——DropDownList
1.html <asp:CheckBox ID="CheckBox11" runat="server" onclick="changecheck ...
- magnetom模板制作
我个人认为Magento模板制作的难点在于不了解Magento的架构,不会调动block.Magento的block调动几乎都是靠xml.在下面的内容会提及如何操作. 制作Magento模板的前提是: ...
- Python MySQL ORM QuickORM hacking
# coding: utf-8 # # Python MySQL ORM QuickORM hacking # 说明: # 以前仅仅是知道有ORM的存在,但是对ORM这个东西内部工作原理不是很清楚, ...
- 云数据库 Bmob
/**************************************************************************************** * 云数据库 Bmo ...