POJ 1149:PIGS 网络流经典题
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 18345 | Accepted: 8354 |
Description
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 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
Mirko工作在一个有M间猪房的农场里,但是他没有钥匙不能打开这些房间。有趣的是,顾客有要买哪些猪的房间的钥匙。购买的时候Mirko就可以打开相应的房间,然后也可以调整相应房间里猪的数量关系。问在一定顾客与购买关系的条件下,Mirko最多能卖多少猪。
这个构图求最大网络流的想法太赞,我真是远远想不到。
构造图,构造一个源点S和汇点t。
顾客是除了源点和汇点以外的节点,并且把每个猪房的第一个顾客与源点相连,权值为该猪房的起始猪的数量,假设第一个购买第N个猪房与第一个购买第M个猪房的人是一个人,则将其合并成一个点。
顾客I在顾客J之后购买相同猪房中的猪,则有一条边从顾客J指向顾客I,权值为无穷大,因为是要从那个边过来的网络流中取一部分。(这种构图方法谁想的,太厉害了。)
每一个顾客都与汇点连一条边,边的权值是每一个顾客希望购买的猪的数量。
这种构图方式最让我瞠目结舌的在于它居然就真的全照顾到了题目所要求的所有条件,一开始我觉得这些条件真多真繁琐啊,构图怎么可能直接求一个网络流就完事了呢,以为图论的问题都是像之前那么直接套模板了,结果这种构图方式,哇~
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; const int sum = 201;
const int INF = 99999999; int cap[sum][sum],flow[sum][sum],a[sum],p[sum];
int val[1005];
int flag[1005];
int N,M; void Edmonds_Karp()
{
int u,t,result=0;
queue <int> s;
while(s.size())s.pop(); while(1)
{
memset(a,0,sizeof(a));
memset(p,0,sizeof(p)); a[0]=INF;
s.push(0); while(s.size())
{
u=s.front();
s.pop(); for(t=0;t<=M+1;t++)
{
if(!a[t]&&flow[u][t]<cap[u][t])
{
s.push(t);
p[t]=u;
a[t]=min(a[u],cap[u][t]-flow[u][t]);//要和之前的那个点,逐一比较,到M时就是整个路径的最小残量
}
}
}
if(a[M+1]==0)
break;
result += a[M+1]; for(u=M+1;u!=0;u=p[u])
{
flow[p[u]][u] += a[M+1];
flow[u][p[u]] -= a[M+1];
}
}
cout<<result<<endl;
} int main()
{
int i,j,num,out,u,v,value;
while(scanf("%d%d",&N,&M)==2)
{
memset(cap,0,sizeof(cap));
memset(flow,0,sizeof(flow));
memset(flag,0,sizeof(flag)); for(i=1;i<=N;i++)
{
scanf("%d",&val[i]);
} for(i=1;i<=M;i++)
{
scanf("%d",&num);
for(j=1;j<=num;j++)
{
scanf("%d",&value);
if(!flag[value])
{
flag[value]=i;
cap[0][i] += val[value];
}
else
{
cap[flag[value]][i] = INF;
flag[value]=i;
}
}
scanf("%d",&out);
cap[i][M+1] += out;
}
Edmonds_Karp();
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 1149:PIGS 网络流经典题的更多相关文章
- poj 1149 Pigs 网络流-最大流 建图的题目(明天更新)-已更新
题目大意:是有M个猪圈,N个顾客,顾客要买猪,神奇的是顾客有一些猪圈的钥匙而主人MIRKO却没有钥匙,多么神奇?顾客可以在打开的猪圈购买任意数量的猪,只要猪圈里有足够数量的猪.而且当顾客打开猪圈后mi ...
- POJ 1149 PIGS ★(经典网络流构图)
[题意] 有M个猪圈,每个猪圈里初始时有若干头猪.一开始所有猪圈都是关闭的.依 次来了N个顾客,每个顾客分别会打开指定的几个猪圈,从中买若干头猪.每 个顾客分别都有他能够买的数量的上限.每个顾客走后, ...
- poj 3159(差分约束经典题)
题目链接:http://poj.org/problem?id=3159思路:题目意思很简单,都与给定的条件dist[b]-dist[a]<=c,求dist[n]-dist[1]的最大值,显然这是 ...
- POJ 1149 PIGS(Dinic最大流)
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20738 Accepted: 9481 Description ...
- poj 1149 PIGS【最大流经典建图】
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18727 Accepted: 8508 Description ...
- 网络流(最大流):POJ 1149 PIGS
PIGS Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. 64-bit integer(整数) ...
- [poj] 1149 PIGS || 最大流经典题目
原题 题目大意 给你m个猪圈以及每个猪圈里原来有多少头猪,先后给你n个人,每个人能打开一些猪圈并且他们最多想买Ki头猪,在每一个人买完后能将打开的猪圈中的猪顺意分配在这次打开猪圈里,在下一个人来之前 ...
- POJ 1149 PIGS (AC这道题很不容易啊)网络流
PIGS Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlo ...
- POJ 1149 PIGS 【网络流】
题意: m n //有m个猪圈,n个人卖猪. a1...am //编号为i的猪圈里有ai头猪. b1 c1...cb1 d1 //第i个人有bi把钥匙,分别是ci猪圈的,其它猪圈里的猪都 ...
随机推荐
- springCloud 之 Eureka服务治理机制及代码运行
服务提供者 服务注册: 服务提供者在启动的时候通过发送Rest请求的方式将自己注册到Eureka Server上,同时带上了自身服务的一些元数据信息.Eureka Server在收到这个请求后,将元数 ...
- 微信小程序支付功能前端流程
只是分享一下小程序支付功能的前端流程和代码, 仅供参考(使用的是uni app). handleCreate () { /** 第一步:前台将商品数据发送到后台,后台创建订单入库并返回订单id等信息 ...
- Day5-T3
原题目 要开运动会了,神犇学校的n个班级要选班服,班服共有100种样式,编号1~100.现在每个班都挑出了一些样式待选,每个班最多有100个待选的样式.要求每个班最终选定一种样式作为班服,且该班的样式 ...
- jenkins#安装gitlab
通过docker安装gitlab 参考 ------------------------------ 拉docker 镜像: docker pull gitlab/gitlab-ce 创建目录存储gi ...
- Day6 - E - Brownie Points II POJ - 2464
Stan and Ollie play the game of Odd Brownie Points. Some brownie points are located in the plane, at ...
- gulp和npm等安装
提前安装了node.js, https://nodejs.org/zh-cn/download/ 跟着提示安装就行,然后执行一下命令cdm看下版本号如下图就说明安装成功了 安装包里面集成了npm,然后 ...
- MQ的调用
mq调用(相关dll) using RabbitMQ.Client; using RabbitMQ.Client.Events; using System; using System.Collecti ...
- java代码静态分析工具
需求:有些基础类型的遍历,值希望它被赋予指定的几个值,赋予其他值能够别编译器(IDEA/eclipse)提醒 Android studu结合自己的插件,引入 <!-- https://mvnre ...
- 《ES6标准入门》(阮一峰)--7.数值的扩展
1.二进制和八进制表示法 ES6 提供了二进制和八进制数值的新的写法,分别用前缀0b(或0B)和0o(或0O)表示. 0b111110111 === 503 // true 0o767 === 503 ...
- 学校算法作业X——(日期问题)
最近一直在忙项目,难得有时间写一下作业,所以断了,现在赶紧续上 题目如下: 日历问题 问题描述 在我们现在使用的日历中, 闰年被定义为能被4整除的年份,但是能被100整除而不能被400整除的年是例外, ...