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这道题很不容易啊)网络流的更多相关文章

  1. poj 1149 Pigs 网络流-最大流 建图的题目(明天更新)-已更新

    题目大意:是有M个猪圈,N个顾客,顾客要买猪,神奇的是顾客有一些猪圈的钥匙而主人MIRKO却没有钥匙,多么神奇?顾客可以在打开的猪圈购买任意数量的猪,只要猪圈里有足够数量的猪.而且当顾客打开猪圈后mi ...

  2. POJ 1149 PIGS(Dinic最大流)

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20738   Accepted: 9481 Description ...

  3. POJ 1149 - PIGS - [最大流构图]

    Time Limit: 1000MS Memory Limit: 10000K Description Mirko works on a pig farm that consists of M loc ...

  4. POJ 1149 PIGS(最大流)

    Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock an ...

  5. POJ 1149 PIGS

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20579   Accepted: 9387 Description ...

  6. POJ 1149 PIGS ★(经典网络流构图)

    [题意] 有M个猪圈,每个猪圈里初始时有若干头猪.一开始所有猪圈都是关闭的.依 次来了N个顾客,每个顾客分别会打开指定的几个猪圈,从中买若干头猪.每 个顾客分别都有他能够买的数量的上限.每个顾客走后, ...

  7. poj 1149 PIGS【最大流经典建图】

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18727   Accepted: 8508 Description ...

  8. 网络流(最大流):POJ 1149 PIGS

    PIGS Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. 64-bit integer(整数) ...

  9. poj 1149 PIGS(最大流经典构图)

    题目描述:迈克在一个养猪场工作,养猪场里有M 个猪圈,每个猪圈都上了锁.由于迈克没有钥匙,所以他不能打开任何一个猪圈.要买猪的顾客一个接一个来到养猪场,每个顾客有一些猪圈的钥匙,而且他们要买一定数量的 ...

随机推荐

  1. spring IOC bean间关系

    1.0 继承关系 实体 package com.java.test5; import java.util.*; /** * @author nidegui * @create 2019-06-22 1 ...

  2. VMware或者KVM克隆的虚拟机网卡无法启动

    在VMware里克隆出来的CentOS Linux.. ifconfig...没有看到eth0..然后重启网卡又报下面错误. 故障现象: service network restartShutting ...

  3. MATLAB仿真学习笔记(一)

    一.Simulink概述 1.特点 simulink是对动态系统进行建模.仿真和综合分析的图形化软件,可以处理线性和非线性.离散.连续和混合系统,也可以处理单任务和多任务系统,并支持多种采样频率的系统 ...

  4. 猜数字游戏的提示(Master-Mind Hints, UVa 340)

    实现一个经典"猜数字"游戏. 给定答案序列和用户猜的序列,统计有多少数字位置正确 (A),有多少数字在两个序列都出现过但位置不对(B). 输入包含多组数据.每组输入第一行为序列长度 ...

  5. [LUOGU]4932 浏览器

    \(\_\_stdcall\)大佬出的题\(Orz\) 我们惊奇地发现,加入\(\_\_popcount(x)\)和\(\_\_popcount(y)\)的奇偶数性相同,那么\(\_\_popcoun ...

  6. [pytorch学习]1.pytorch ubuntu安装

    看完了Deep Learning with Python,尝试了部分Keras的demo代码. 感觉Keras虽然容易上手,能够快速搭建出一个通用的模型,但是缺乏对底层的控制. 同时,在使用了自己编译 ...

  7. 瑞芯微ROCK960 RK3399固件烧录总结

    1 下载固件 进入瑞芯微ROCK960下载主页 https://www.96boards.org/documentation/consumer/rock/downloads/ 选择os固件, Debi ...

  8. VMware Workstation搭建Linux操作系统

    1.单击“创建新的虚拟机”选项,并在弹出的“新建虚拟机向导”界面中选择“自定义”单选按钮,然后单击“下一步”. 新建虚拟机向导 2.选择虚拟机硬件兼容性,是否兼容之前旧的版本. 兼容性选择 3.选中“ ...

  9. Delphi 10.3.2最新消息

    官方已经发布消息,招内测人员了! https://www.barnsten.com/default/newsupdates/details?news_id=328 https://docs.googl ...

  10. hdu2013 蟠桃记【C++】

    蟠桃记 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...