PIGS
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19575   Accepted: 8948

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

Source

网络流好题。有 M 个猪圈(M ≤ 1000),每个猪圈里初始时有若干头猪。 一开始所有猪圈都是关闭的。依次来了 N 个顾客(N ≤ 100),每个顾客分别会打开指定的几个猪圈,从中买若干头猪。每个顾客分别都有他能够买的数量的上限。每个顾客走后,他打开的那些猪圈中的猪,都可以被任意地调换到其它开着的猪圈里,然后所有猪圈重新关上。问总共最多能卖出多少头猪。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const int big=50000;
int max(int a,int b) {return a>b?a:b;};
int min(int a,int b) {return a<b?a:b;};
struct edge{
int to,cap,rev;
}; vector<edge> G[105];
vector<int> topig[1005];
int n,m,want[105],pignum[1005],cap[105],level[105],iter[105]; void add_edge(int u,int v,int cap)
{
G[u].push_back(edge{v,cap,G[v].size()});
G[v].push_back(edge{u,0,G[u].size()-1});
} void bfs(int s)
{
queue<int> q;
q.push(s);
level[s]=1;
while(q.size())
{
int now=q.front();q.pop();
for(int i=0;i<G[now].size();i++)
if(G[now][i].cap>0)
{
edge e=G[now][i];
if(level[e.to]<0)
{
level[e.to]=level[now]+1;
q.push(e.to);
}
}
}
}
int dfs(int s,int t,int minn)
{
if(s==t)
return minn;
for(int &i=iter[s];i<G[s].size();i++)
{
edge &e=G[s][i];
if(level[e.to]>level[s]&&e.cap>0)
{
int k=dfs(e.to,t,min(minn,e.cap));
if(k>0)
{
e.cap-=k;
G[e.to][e.rev].cap+=k;
return k;
}
}
}
return 0;
} int max_flow(int s,int t)
{
int ans=0,temp;
for(;;)
{
memset(level,-1,sizeof(level));
bfs(s);
if(level[t]<0)
return ans;
memset(iter,0,sizeof(iter));
while((temp=dfs(s,t,inf))>0)
ans+=temp;
}
return ans;
} void buildgraph()
{
for(int i=1;i<=m;i++)
if(topig[i].size())
{
int man=topig[i][0];
cap[man]+=pignum[i];
for(int j=1;j<topig[i].size();j++)
add_edge(topig[i][j-1],topig[i][j],inf);
} for(int i=1;i<=n;i++)
{
add_edge(0,i,cap[i]);
add_edge(i,n+1,want[i]);
}
} void init()
{
MM(cap,0);
for(int i=0;i<n+1;i++) G[i].clear();
for(int i=1;i<=m;i++) topig[i].clear();
} int main()
{
while(~scanf("%d %d",&m,&n))
{
init();
for(int i=1;i<=m;i++)
scanf("%d",&pignum[i]);
for(int i=1;i<=n;i++)
{
int num,pig;
scanf("%d",&num);
for(int j=1;j<=num;j++)
{
scanf("%d",&pig);
topig[pig].push_back(i);
}
scanf("%d",&want[i]);
} buildgraph(); printf("%d\n",max_flow(0,n+1));
}
return 0;
}

  参考了这篇题解

POJ 1149 猪圈买猪 建图太强大!! 没有透彻领悟 慢慢消化的更多相关文章

  1. POJ 3687 Labeling Balls 逆向建图,拓扑排序

    题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...

  2. poj 3026 Borg Maze bfs建图+最小生成树

    题目说从S开始,在S或者A的地方可以分裂前进. 想一想后发现就是求一颗最小生成树. 首先bfs预处理得到每两点之间的距离,我的程序用map做了一个映射,将每个点的坐标映射到1-n上,这样建图比较方便. ...

  3. poj 3678 Katu Puzzle 2-SAT 建图入门

    Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...

  4. POJ 1556 - The Doors - [平面几何+建图spfa最短路]

    题目链接:http://poj.org/problem?id=1556 Time Limit: 1000MS Memory Limit: 10000K Description You are to f ...

  5. poj 2226 Muddy Fields(合理建图+二分匹配)

    /* 题意:用木板盖住泥泞的地方,不能盖住草.木板任意长!可以重叠覆盖! '*'表示泥泞的地方,'.'表示草! 思路: 首先让我们回忆一下HDU 2119 Matrix这一道题,一个矩阵中只有0, 1 ...

  6. POJ 2049 Finding Nemo bfs 建图很难。。

    Finding Nemo Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 6952   Accepted: 1584 Desc ...

  7. POJ 3281 Dining ( 最大流 && 建图 )

    题意 : 有 N 头牛,John 可以制作 F 种食物和 D 种饮料, 然后接下来有 N 行,每行代表一头牛的喜好==>开头两个数 Fi 和 Di 表示这头牛喜欢 Fi 种食物, Di  种饮料 ...

  8. PIGS POJ - 1149网络流(最短增广路---广搜) + 建图

    题意: 第一行输入m和n,m是猪圈的数量,n是顾客的数量,下面n行 第 i+1行表示第i个顾客 , 输入第一个数字表示有几把猪圈的钥匙,后面输入对应的猪圈,最后一个数字输入顾客想买几头猪. 建图: 设 ...

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

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

随机推荐

  1. FFmpeg4.0笔记:封装ffmpeg的视频帧转换功能类CSws

    Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff CSws.h /************************* ...

  2. python3.7环境下创建app,运行Django1.11版本项目报错SyntaxError: Generator expression must be parenthesized

    咳咳!!! 今天用命令行创建django项目中的app应用,出现了这样一个错误 这个错误在python3.6版本下安装运行django 1.11版本正常运行,但python3.7版本下运行django ...

  3. FullPage.js全屏插件文档及使用方法

    简介 fullPage.js是一个基于jQuery的全屏滚动插件,它能够很方便.很轻松的制作出全屏网站 下载地址 下载地址 相关示例:基于fullpage.js实现的360全屏滑动效果 支持功能 支持 ...

  4. leetcode题库

    leetcode题库 #题名题解通过率难度出现频率  1 两数之和     46.5%简单2 两数相加     35.5%中等3 无重复字符的最长子串     31.1%中等4 寻找两个有序数组的中位 ...

  5. pc端和移动端的“窗口”(viewport)故事(part1)

    A tale of two viewports - part one 在以下的系列文章中,我将为大家解释浏览器中的视窗和一些重要的元素的尺寸是如何起作用的,如:大家最熟悉的html元素以及window ...

  6. 导出excel-文件流

    后台代码(控制器或者dao层) public void Excel(){ //第一步:(1)以虚拟表的形式获取导出数据 DataTable dt_0 = this.service.Excel(); / ...

  7. maven 学习之路一

    一.mave介绍: maven :我的理解就是一个代码构建管理的一个工具.类似的工具有gradle,ant等. 官方理解:Apache Maven is a software project mana ...

  8. eclipse查看jsp出现failed to create the part's controls的解决方法

    问题描述 最近eclipse出现了一下问题,某个类无法打开,显示Failed to create the part’s controls ,如图: 问题解决 安装的JDK 版本估计有冲突导致IDE出现 ...

  9. 《编译原理》构造与正规式 (0|1)*01 等价的 DFA - 例题解析

    <编译原理>构造与正规式 (0|1)*01 等价的 DFA - 例题解析 解题步骤: NFA 状态转换图 子集法 DFA 的状态转换矩阵 DFA 的状态转图 解: 已给正规式:(0|1)* ...

  10. 1.Go-copy函数、sort排序、双向链表、list操作和双向循环链表

    1.1.copy函数 通过copy函数可以把一个切片内容复制到另一个切片中 (1)把长切片拷贝到短切片中 ? 1 2 3 4 5 6 7 8 9 10 11 12 package main   imp ...