PIGS
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 24426 Accepted: 11127
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

题意:就是一个养猪户卖猪,有M个房间,猪在房间里锁着,但是自己身上没有钥匙,买猪的人有N个,每个人手上有A个钥匙,分别是K1,K2,K3。。。KA房间的钥匙,所以可以买这些房间里面的猪,买猪的人需要B头猪。每一次来买猪的人买完后,养猪户都可以把其中一些房间的猪赶到另外一些房间里面去,这些房间都是这次买猪的人能打开的,而且每一间的房间可以装无限多的猪。
当时少看了两个条件,去看博客的时候看题意才懂的,一个是可以赶猪,另一个就是按照顺序来买猪,不然就会2号买猪先买,然后把3房间的猪赶到2房间,然后所有人都可以买到猪了,与样例答案不符。所以这个是按照顺序来买猪的。
问总共可以卖多少头猪

样例解释:3个房间,分别3 1 10 头猪。
3个买猪人,第一个2个钥匙,能开1 2 房间,需要2头猪
后面不解释
然后可以养猪户总共卖了7头猪
首先第一个买猪,买了1房间两头猪,然后养猪户把1房间剩下的1头猪赶到2房间,此时2房间有2头猪
第二个买猪的买3房间3头猪
第三个买2房间的2头猪
总共7头

思路:网络流,从源点到每个买猪人建边,流量是需要的猪的数量,每个买猪到能开的房间建边,流量是原来房间猪的数量,房间到汇点建边,流量是原来房间猪的数量。
第一个不解释,第二个是原来房间猪的数量,意思是:因为赶猪的条件,每个房间的猪可能会赶到其他房间,后来的买猪人会买到这些猪,但是把猪的剩余数量流到其他房间显得不太可能,**于是我们可以把有相同钥匙(即能开同一房间)的人建一条边,流量是INF,**这样,我们就可以转换一下,既然你可以把猪都赶到一个房间,那么就可以赶到我们相同的房间,那么我想买多少头猪,就可以通过你的途径来买,于是这条边是后来的指向先到的,流量是INF,因为还有后来的可以通过我的途径然后我通过你的途径来买猪~~(禁止套娃)~~ 第三个不解释,然后网络流套板子就可以了。
AC代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstdio>
using namespace std;
const int maxm=5e5+10;
const int maxn=3000;
const int INF=0x7f7f7f7f;
struct Edge{int next,v,val;
}map[maxm];
int head[maxn],sum;
int n,m,S,E,maxflow;
int num[maxn],dep[maxn],gap[maxn];void Init()
{memset(map,0,sizeof(map));memset(head,-1,sizeof(head));sum=0;return ;
}
void addedge(int u,int v,int w)
{map[sum].v=v;map[sum].val=w;map[sum].next=head[u];head[u]=sum++;map[sum].v=u;map[sum].val=0;map[sum].next=head[v];head[v]=sum++;return ;
}
void bfs()
{memset(dep,-1,sizeof(dep));memset(gap,0,sizeof(gap));dep[E]=0;gap[0]=1;queue<int> q;q.push(E);while (!q.empty()){int u=q.front();q.pop();for (int i=head[u];i!=-1;i=map[i].next){int v=map[i].v;if (dep[v]!=-1) continue;q.push(v);dep[v]=dep[u]+1;gap[dep[v]]++;}}//cout<<dep[S]<<"---------"<<endl;return ;
}
int dfs(int u,int flow)
{if (u==E){maxflow+=flow;//cout<<maxflow<<" --== ==-- "<<endl;  //cout<<dep[S]<<endl;return flow;}int used=0;for (int i=head[u];i!=-1;i=map[i].next){int v=map[i].v;if (map[i].val>0&&dep[v]+1==dep[u]){//cout<<u<<" -> "<<v<<" "<<map[i].val<<endl;int meta=dfs(v,min(map[i].val,flow-used));if (meta>0){map[i].val-=meta;map[i^1].val+=meta;used+=meta;}if (used==flow) return used; }}--gap[dep[u]];if (gap[dep[u]]==0) dep[S]=E+1;dep[u]++;gap[dep[u]]++;return used;
}
int ISAP()
{maxflow=0;bfs();while (dep[S]<E) dfs(S,INF);return maxflow;
}int main(){vector<int> bri[maxn];int fl[maxn];int w,k;cin>>m>>n;S=0;E=n+m+1;Init();for (int i=1;i<=m;i++){scanf("%d",&fl[i]);   //记录每个房间原来猪的数量addedge(n+i,E,fl[i]);    //房间和汇点建边}for (int i=1;i<=n;i++){scanf("%d",&k);for (int j=1;j<=k;j++) {scanf("%d",&num[j]);  bri[num[j]].push_back(i);   //记录可以打开房间的买猪人}scanf("%d",&w);for (int j=1;j<=k;j++) addedge(i,n+num[j],fl[num[j]]);//买猪人和房间建边addedge(S,i,w);     //源点和买猪人建边}for (int i=1;i<=m;i++){k=bri[i].size();for (int j=k-1;j>0;j--)addedge(bri[i][j],bri[i][j-1],INF);    //从后往前人人之间建边}int ans=ISAP();cout<<ans<<endl;return 0;
}

POJ-1149网络流相关推荐

  1. [Poj 1459] 网络流(一) {基本概念与算法}

    { 凸包的内容还欠整理 先来侃侃一个月以前就想写写的网络流 本文介绍网络流 网络流的算法 及其应用 这些问题没事想想还是很有意思的 } ================================ ...

  2. poj 3469(网络流模版)

    题目链接:http://poj.org/problem?id=3469 思路:终于把网络流的模版测试好了,在Dinic和Sap之间还是选择了Sap,事实证明Sap确实比Dinic效率高,在此贴出自己的 ...

  3. B - Dining POJ - 3281 -网络流拆点模板

    B - Dining POJ - 3281 题意:一些牛,每只牛有 一些  想吃的food and milk,然后问最大能够满足多少只牛. 满足是指的这头牛即能吃到他喜欢吃的又能喝到他喜欢喝的 思路: ...

  4. poj 1149 PIGS【最大流】

    建图:s向所有猪圈的第一个顾客连流量为这个猪圈里住的数量,然后对于之后每个来这个猪圈的顾客,由他前一个顾客向他连边权为无穷的边,然后每个顾客向t连流量为这个顾客购买上限的边.然后跑最大流 #inclu ...

  5. POJ 1149 PIGS

    POJ_1149 这个题目搞得我比较纠结,具体的思想还是看看这篇博客吧. http://imlazy.ycool.com/post.2059102.html #include<stdio.h&g ...

  6. POJ 1149 最大流建图 PIGS

    题意: 给出猪圈个数 m 和买家人数 n 然后给出m个猪圈的猪的头数.. 接下来 n 行.. 给出mm a1 a2 .. a(mm) k 例如 2 1 5 3 表示第i+1个用户 有mm(2) 个猪圈 ...

  7. POJ 1149(最大流)

    这道题应该都能想到朴素的有n*m+个点的建图方案吧,呵呵,显然是不行的. 那么怎么办? 其实我们可以这样想:一个人能买到的猪有两个来源: ①来自自己第一次打开的猪圈 ②来自之前别人打开的猪圈 想到了这 ...

  8. Dining POJ - 3281 (网络流)

    传送门 题意:农夫约翰为他的N头牛准备了F种食物和D种饮料.每头牛都有各自喜欢的食物和饮料,而每种食物或饮料只能分配给一头牛.最多能有多少头牛同时得到自己喜欢的食物和饮料? 题解:如果只是分配食物的话 ...

  9. poj 1149 PIG

    建图才是王道,建完图就是裸最大流问题了 建图参考: http://wenku.baidu.com/view/0ad00abec77da26925c5b01c.html /*最大流问题*/ #inclu ...

  10. POJ 1149 PIGS 最大流建模

    点击打开链接 PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14204   Accepted: 6305 Desc ...

最新文章

  1. Elixir 1.2带来多项功能增强和性能提升
  2. HTML的BODY内标签介绍
  3. 查找 framework 文件中是否包含 WKWebView
  4. HDU 2282 Chocolate (最小费用最大流)
  5. MySQL自学2018/05/02-数据类型
  6. Google Protocol Buffers 2.3.0 for java 快速开始
  7. C++primer 13.6.2节练习
  8. 太沉重了:中国获全球“人道主义摄影奖”的照片!组图
  9. 笔记(2015.8.1)
  10. Python 函数也是一种对象
  11. 管理感悟:把公司搞死,然后抱怨融资困难
  12. 算法设计与分析(1)——基础知识
  13. BP神经网络模型---第一篇(M-P模型)
  14. 在哪里可以搜索英文文献?
  15. 编译原理(紫龙书)第2版习题答案
  16. 基因组注释2. 非编码基因和编码基因预测tRNAScan-SE、rRNAmmer和Prodigal
  17. php 加号转义,URL中加号(+)转义问题
  18. ubutun 滑动 触控板_Ubuntu系统的笔记本触摸板怎么调节鼠标光标速度?
  19. Excel筛选两列重复的内容
  20. 使用递归方法查询所有分类(一)

热门文章

  1. 如果编程语言是一门武功绝学
  2. Vuex实战之 todos待办事项列表的状态管理
  3. 华为养狼,喂的是真肉
  4. PPT处理控件Aspose.Slides功能演示:使用 Java 在 PowerPoint 中创建和操作表格
  5. 美通企业日报 | 易车收到腾讯等私有化要约;沃尔玛中国推出快时尚品牌George...
  6. JN5169 NXP ZigBee PRO 无线网络应用所需的常见操作(一)
  7. spurious wakeup -- 多线程之虚假唤醒
  8. 飞信免费发短信API接口
  9. win10添加惠普hp laserjet 1010HB打印机
  10. java 图片 文字_java实现 给图片加上文字