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. 发布自己的nuget包

    1.先到www.nuget.org注册账户,然后在用户中心获取apikey 2.到https://dist.nuget.org/index.html下载最新的nuget.exe,放到你的项目根目录下 ...

  2. redis键的过期和内存淘汰策略

    键的过期时间 设置过期时间 Redis可以为存储在数据库中的值设置过期时间,作为一个缓存数据库,这个特性是很有帮助的.我们项目中的token或其他登录信息,尤其是短信验证码都是有时间限制的. 按照传统 ...

  3. 创建pod索引库(Specs)

    专门用来存放xxx.podspec 的索引文件的库就叫做索引库.我们需要将这些索引文件上传到远程索引库才能保证其他的人能够拿来用. 创建一个远程索引库和本地索引库对应起来,步骤如下: 1.登录开源中国 ...

  4. react typescript 子组件调用父组件

    //父组件 import * as React from 'react'import { Input } from 'antd'const Search = Input.Searchimport &q ...

  5. 1 WebService 常见问题

    <binding name="> <readerQuotas maxStringContentLength=" /> </binding> &l ...

  6. vue数据绑定源码

    思路分析 数据的双向绑定,就是数据变化了自动更新视图,视图变化了自动更新数据,实际上视图变化更新数据只要通过事件监听就可以实现了,并不是数据双向绑定的关键点.关键还是数据变化了驱动视图自动更新. 所有 ...

  7. Maven pom 配置简介

    1. groupId artifactId version 2. dependencies 3. plugins http://shmilyaw-hotmail-com.iteye.com/blog/ ...

  8. 绿色地址栏扩展验证(EV)SSL证书、支持SGC 强制最低128位

      Pro With EV SSL证书,最严格的域名所有权和企业身份信息验证,属于最高信任级别.最高安全级别的 EV SSL证书,该证书可以使地址栏变成高安全绿色,并且在地址栏内显示您公司的名称,提高 ...

  9. linux -- 视频尺寸-cif、2cif、dcif、D1、HD1、4D1

    1 CIF简介     CIF是常用的标准化图像格式(Common Intermediate Format).在H.323协议簇中,规定了视频采集设备的标准采集分辨率.CIF = 352×288像素 ...

  10. 运维系列之一 Linux的文件与目录权限解析

    在Linux中,万事万物皆文件,普通文件是文件,目录是文件,硬件设备也是文件,因此学习了解Linux中的文件非常重要. Linux中有三种文件类型: (1) 普通文件:又分为文本文件和二进制文件 (2 ...