PIGS
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20146   Accepted: 9218

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
【分析】直接套的书上的标号法模板。
#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 (网络流)的更多相关文章

  1. 【poj1149】 pigs 网络流最大流问题

    描述 Description 尼克在一家养猪场工作,这家养猪场共有M间锁起来的猪舍,由于猪舍的钥匙都给了客户,所以尼克没有办法打开这些猪舍,客户们从早上开始一个接一个来购买生猪,他们到达后首先用手中的 ...

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

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

  3. poj1149 PIGS 最大流(神奇的建图)

    一开始不看题解,建图出错了.后来发现是题目理解错了.  if Mirko wants, he can redistribute the remaining pigs across the unlock ...

  4. POJ1149 PIGS 【最大流 + 构图】

    题目链接:http://poj.org/problem?id=1149 PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...

  5. POJ 1149:PIGS 网络流经典题

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18345   Accepted: 8354 Description ...

  6. POJ1149 PIGS [最大流 建图]

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20662   Accepted: 9435 Description ...

  7. POJ1149 PIGS

    想了好久啊...(#-.-) 开始想到m*n个点的构图,明显超时,于是考虑压缩节点个数 我们发现每个猪圈最后被有且只有一个人调整,于是想到对于一个人,连接他能调整的每个猪圈的上一个控制人.(不懂可以开 ...

  8. POJ1149 PIGS 【最大流量】

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16555   Accepted: 7416 Description ...

  9. BZOJ1280 Emmy卖猪pigs 网络流

    正解:网络流 解题报告: 传送门! 我网络流的基础题都还麻油做完就来做这个了,,,wsl,,, 首先想下最基础的构图方法 不难想到把猪圈和顾客分别当做节点,然后新建一个源点和汇点 然后考虑怎么连边,首 ...

随机推荐

  1. RPI学习--wiringpi_API

    reference: https://projects.drogon.net/raspberry-pi/wiringpi/functions/ Functions (API) Some of the ...

  2. xlistview的XML(脚)xlistview_footer

    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" ...

  3. Oracle GoldenGate: 使用宏

         OGG宏与C语言中的宏一样,提供了函数封装的功能,即可以将一些配置参数整理为一个宏,然后在多个参数文件中共用,针对环境复杂或多个复制点的情况尤其有用.下面我们将介绍如何创建一个宏的库,以及在 ...

  4. MongoDB的C#驱动程序教程(译) 转

    1.概述 本教程是10gen支持C#驱动程序MongoDB的介绍.假定您熟悉使用MongoDB,因此主要集中在如何使用C#访问MongoDB的. 它分为两个部分:C# 驱动程序 ,BSON图书馆.C# ...

  5. lightoj1085 线段树+dp

    //Accepted 7552 KB 844 ms //dp[i]=sum(dp[j])+1 j<i && a[j]<a[i] //可以用线段树求所用小于a[i]的dp[j ...

  6. 2016 - 1- 19 NSOperationQueue的简单使用

    一:NSOperationQueue的作用: 1.NSOperation可以调用start方法来执行任务,但默认是同步执行. 2.如果将NSOperation加入到NSOperationQueue中, ...

  7. Android.mk 常用宏和变量

    android ndk开发有一个重要的文件 Android.mk,他虽然重要,但是对它进行深入介绍的文档却比较的少,这里将对Android.mk中常用的宏和变量进行说明: 由于这一部分的内容多,资料零 ...

  8. 转载 javascript中的正则表达式总结 二

    学习正则表达式 今年的第一篇javascript文章就是这个正则表达式了,之前的文章是转载别人的,不算自己的东西,可以忽略不计,最近突然想把转载别人的东西 统统删掉,因为转载过的文章,我根本没有从中获 ...

  9. HDOJ-三部曲一(搜索、数学)-1012-Shredding Company

    Shredding Company Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) T ...

  10. 并列div自动等高

    并列div自动等高 方法一:css控制 <!DOCTYPE html> <html lang="en"> <head> <meta cha ...