POJ 1149 PIGS (AC这道题很不容易啊)网络流
PIGS
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
题意:
输入第一行给你n个猪圈,m个顾客。
第二行n个数,分别表示第i个猪圈里有多少头猪。
然后后m行,每行第一个数表示第i个人有k把钥匙,后k个数为他的钥匙能开的猪圈标号。最后一个数表示他需要多少头猪。
所有开着门的猪圈里的猪能相互串。(人买走猪后就把猪圈锁上了)
求最多能卖多少头猪。
思路:
一开始 没有思路,想了好久,终于建好图了(细节请见代码注释) 。接着就无限WA。WA。WA。憋了一下午。晚上李队长帮忙挑错,后来LH也加入挑错的行列,挑了1h,才挑出来。(对拍拍了快1000组数据也没有拍出错)。
原来是我没有建反向的流。反向流是无论什么情况都要建的。。
下面是需要返向流的原因。
假设有个这个图。
这个图如果不建反向流的话就是错的。然而对拍拍不出来。。。
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int houses,customers,num[2005],vis[2005],tot=0,a[2005],jy,p[2005];
int first[2005],nxt[2005*2005],w[2005*2005],v[2005*2005];
void add(int from,int to,int weight)//ATTENTION!!!!!!需要建反向边!!! (多谢LH大神的指点)
{
v[tot]=to;w[tot]=weight;//否则是没有办法回溯的 无论是不是DAG都是这样。。。
nxt[tot]=first[from];
first[from]=tot++;
v[tot]=from;w[tot]=0;
nxt[tot]=first[to];
first[to]=tot++;
}
int maxflow()
{
queue<int>q;int f=0;
while(1)
{
memset(vis,0,sizeof(vis));
memset(a,0,sizeof(a));
q.push(0);vis[0]=1;a[0]=0x3fffffff;
while(!q.empty()){
jy=q.front();q.pop();
for(int i=first[jy];~i;i=nxt[i]){
if(!vis[v[i]]&&w[i]){
vis[v[i]]=1;
a[v[i]]=min(a[jy],w[i]);
p[ v[i] ]=i;//找 上次 改流的边 。。。
q.push(v[i]);
}
}
}
if(a[2004]==0)break;
for(int i=2004;i;i=v[ p[i]^1 ])
w[ p[i] ]-=a[2004],w[ p[i]^1 ]+=a[2004];//反向边要 “- ”
f+=a[2004];
}
return f;
}
int main()
{
scanf("%d%d",&houses,&customers);// houses 1 <= M <= 1000 customers 1 <= N <= 100
memset(first,-1,sizeof(first));
for(int i=1;i<=houses;i++)
scanf("%d",&num[i]);
for(int i=1;i<=houses;i++)//建一个超级源点“0”。
add(0,i,0x3fffffff);
int A,B,xx;
for(int i=1006;i<=customers+1005;i++)//第i个customer
{
scanf("%d",&A);
for(int j=1;j<=A;j++)//第i个customer 拥有的钥匙
{
scanf("%d",&xx);//钥匙号
if(!vis[xx])//如果没有被打开过,直接把猪圈和商人之间连上边权为猪圈里面猪的数量的边
vis[xx]=i,add(xx,i,num[xx]);//xx---->猪圈号 i----->商人号
else//如果它被打开过,把打开过该猪圈的最后一个商人和此商人连边。
add(vis[xx],i,0x3fffffff),vis[xx]=i;//vis[xx]----> 打开过该猪圈的最后一个商人!最后一个商人!否则是错的(多谢李队长指点。。。)
}
scanf("%d",&B);
add(i,2004,B);//设点2004为超级终点
}
printf("%d\n",maxflow());
}
一下午+一晚上 。。。。
POJ 1149 PIGS (AC这道题很不容易啊)网络流的更多相关文章
- poj 1149 Pigs 网络流-最大流 建图的题目(明天更新)-已更新
题目大意:是有M个猪圈,N个顾客,顾客要买猪,神奇的是顾客有一些猪圈的钥匙而主人MIRKO却没有钥匙,多么神奇?顾客可以在打开的猪圈购买任意数量的猪,只要猪圈里有足够数量的猪.而且当顾客打开猪圈后mi ...
- POJ 1149 PIGS(Dinic最大流)
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20738 Accepted: 9481 Description ...
- POJ 1149 - PIGS - [最大流构图]
Time Limit: 1000MS Memory Limit: 10000K Description Mirko works on a pig farm that consists of M loc ...
- POJ 1149 PIGS(最大流)
Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock an ...
- POJ 1149 PIGS
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20579 Accepted: 9387 Description ...
- POJ 1149 PIGS ★(经典网络流构图)
[题意] 有M个猪圈,每个猪圈里初始时有若干头猪.一开始所有猪圈都是关闭的.依 次来了N个顾客,每个顾客分别会打开指定的几个猪圈,从中买若干头猪.每 个顾客分别都有他能够买的数量的上限.每个顾客走后, ...
- 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 个猪圈,每个猪圈都上了锁.由于迈克没有钥匙,所以他不能打开任何一个猪圈.要买猪的顾客一个接一个来到养猪场,每个顾客有一些猪圈的钥匙,而且他们要买一定数量的 ...
随机推荐
- LNMP动态网站架构及web应用部署,搭建discuz论坛
1)部署Nginx 实验tar安装包可找本人拿记得点+关注,感谢亲们支持,评论拿包 systemctl stop firewalld iptables -F setenforce 0 1)安装支持软件 ...
- Luogu P1041 [2003NOIP提高组]传染病控制
P1041 传染病控制 题目背景 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国大范围流行,该国政府决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完全认识这种传染 ...
- windows桌面远程工具连接Ubuntu
1.Ubuntu安装:sudo apt-get install xrdp sudo apt-get install vnc4server sudo apt-get install xubuntu ...
- js数组对象排序详解
一.js对象遍历输出的时候真的是按照顺序输出吗? 下边就来实践一下: var obj={'3':'ccc',name:'abc',age:23,school:'sdfds',class:'dfd',h ...
- C#--正则匹配
一个好用的Regex测试插件 快捷键:ctrl+ r , ctrl+ x 打开正则表达式工具 C#的正则表达式的常用的规则: [abc] 里面的每一次字符都可以进行匹配 a{2} 匹配2个a a{2, ...
- [bzoj1926][Sdoi2010]粟粟的书架_二分_主席树
粟粟的书架 bzoj-1926 Sdoi-2010 题目大意:题目链接 注释:略 想法:分成两个题 前面的我们可以二分,直接二分出来检验即可. 对于R=1的,相当一个数列,我们在上面建立主席树. 然后 ...
- weblogic 10 无密码启动
首先确定你的domain目录 [c21rms@c21wls10 RMS4]$ pwd/opt/psa/rel/weblogic/RMS4 其次找到下面这个文件夹 servers/AdminServer ...
- MVC.Net 5:允许保存和输出Html内容
当我们在保存表单内容时,如果其中有一项内容包含Html的tag时,系统会报如下错误: A potentially dangerous Request.Form value was detected f ...
- 【转】C语言条件编译及编译预处理阶段
原文: http://www.cnblogs.com/rusty/archive/2011/03/27/1996806.html 1. 宏定义(宏代换,宏替换,宏: 宏定义是C语言提供的3中预处理功能 ...
- js+jquery动态设置/添加/删除/获取元素属性的两种方法集锦对照(动态onclick属性设置+动态title设置)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html140 ...