[poj] 1149 PIGS || 最大流经典题目
原题
题目大意
给你m个猪圈以及每个猪圈里原来有多少头猪,先后给你n个人,每个人能打开一些猪圈并且他们最多想买Ki头猪,在每一个人买完后能将打开的猪圈中的猪顺意分配在这次打开猪圈里,在下一个人来之前 已打开的猪圈被锁上。
解析
以每个买猪的作为点,并建立原点和汇点。
每个猪圈的第一个访客和原点连一条该猪圈中猪数量的边,每个猪圈的访客(除了以第一个)和上一个访问这个猪圈的人连一条inf的边,每个人向汇点连一条最多买的数量的边。
#include<cstdio>
#include<queue>
#define N 1010
#define M 1010
#define inf 0x3f3f3f3f
using namespace std;
int deep[N],head[N],n,m,cnt=2,sum[N],e[M][N],mx[N],cur[N],ans,a[N];
queue <int> q;
struct hhh
{
int to,next,w;
}edge[M*M];
int read()
{
int ans=0,fu=1;
char j=getchar();
for (;(j<'0' || j>'9') && j!='-';j=getchar()) ;
if (j=='-') j=getchar(),fu=-1;
for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0';
return ans*fu;
}
void add(int u,int v,int w)
{
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
bool bfs()
{
for (int i=0;i<=n+1;i++)
cur[i]=head[i],deep[i]=-1;
q.push(0);
deep[0]=1;
while(!q.empty())
{
int r=q.front();
q.pop();
for (int i=head[r],v;i;i=edge[i].next)
{
v=edge[i].to;
if (edge[i].w>0 && deep[v]==-1)
{
deep[v]=deep[r]+1;
q.push(v);
}
}
}
if (deep[n+1]!=-1) return 1;
return 0;
}
int dfs(int x,int f)
{
if (x==n+1) return f;
int ha=0,now;
for (int &i=cur[x],v;i;i=edge[i].next)
{
v=edge[i].to;
if (edge[i].w>0 && deep[v]>deep[x])
{
now=dfs(v,min(f-ha,edge[i].w));
if (now)
{
ha+=now;
edge[i].w-=now;
edge[i^1].w+=now;
}
}
if (ha==f) return ha;
}
return ha;
}
int main()
{
m=read();
n=read();
for (int i=1;i<=m;i++) a[i]=read();
for (int i=1;i<=n;i++)
{
sum[i]=read();
for (int j=1,aa;j<=sum[i];j++)
{
aa=read();
e[aa][++e[aa][0]]=i;
}
mx[i]=read();
add(i,n+1,mx[i]);
add(n+1,i,0);
}
for (int i=1;i<=m;i++)
{
if (e[i][0]) add(0,e[i][1],a[i]),add(e[i][1],0,0);
for (int j=2;j<=e[i][0];j++) add(e[i][j-1],e[i][j],inf),add(e[i][j],e[i][j-1],0);
}
while (bfs()) ans+=dfs(0,inf);
printf("%d\n",ans);
return 0;
}
[poj] 1149 PIGS || 最大流经典题目的更多相关文章
- POJ 1149 - PIGS - [最大流构图]
Time Limit: 1000MS Memory Limit: 10000K Description Mirko works on a pig farm that consists of M loc ...
- poj 1149 pigs(最大流)
题目大意:迈克在农场工作,农场有 m 个猪舍,每个猪舍有若干只猪,但是迈克不能打开任何一间猪舍.有 n 个顾客前来购买,每个顾客有最大的购买数量,每个顾客可以购买某些猪舍的猪,且顾客可以打开这些猪舍, ...
- poj 1149 pigs ---- 最大流
题意以及分析:http://ycool.com/post/zhhrrm6#rule3 主要是建图,简化图,然后在套最大流的模板. #include <iostream> #include& ...
- poj 1149 Pigs 网络流-最大流 建图的题目(明天更新)-已更新
题目大意:是有M个猪圈,N个顾客,顾客要买猪,神奇的是顾客有一些猪圈的钥匙而主人MIRKO却没有钥匙,多么神奇?顾客可以在打开的猪圈购买任意数量的猪,只要猪圈里有足够数量的猪.而且当顾客打开猪圈后mi ...
- POJ 1149 PIGS(Dinic最大流)
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20738 Accepted: 9481 Description ...
- poj 1149 PIGS(最大流经典构图)
题目描述:迈克在一个养猪场工作,养猪场里有M 个猪圈,每个猪圈都上了锁.由于迈克没有钥匙,所以他不能打开任何一个猪圈.要买猪的顾客一个接一个来到养猪场,每个顾客有一些猪圈的钥匙,而且他们要买一定数量的 ...
- poj 1149 PIGS【最大流经典建图】
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18727 Accepted: 8508 Description ...
- POJ 1149 PIGS(最大流)
Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock an ...
- 网络流(最大流):POJ 1149 PIGS
PIGS Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. 64-bit integer(整数) ...
随机推荐
- class 类 this指向的问题
ES6 实现了类的概念 class Prosen { } ES5使用函数模拟 function Prosen() { } ES6中的 class定义一个类, 其内部包含 constructor构造函数 ...
- 利用bootstrap制作滚动监听
滚动监听(Scrollspy)插件,即自动更新导航插件,会根据滚动条的位置自动更新对应的导航目标. 在这里,需要引入三个文件:bootstrap.min.css jquery-3.3.1.j ...
- >题解< 校门外的树
题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是 11 米.我们可以把马路看成一个数轴,马路的一端在数轴 00 的位置,另一端在 LL 的位置:数轴上的每个整数点,即 0,1 ...
- Linux相关知识
1.设置代理 sudo vi /etc/apt/apt.conf Acquire::http::Proxy "http://proxy_address:8080/"; 2.生成 s ...
- Maven - 依赖冲突
依赖冲突有两个规则: 短路优先范例:A -> B -> C -> X-2.0.0A -> D -> X-1.0.0那么A -> X-1.0.0这个版本 先声明优先范 ...
- Percona-Tookit工具包之pt-sift
Preface We've got a lot of files related with system performance which generated by pt-stalk ...
- datatable常用设置
bSort: false, // 是否排序功能 bFilter: false, // 过滤功能 bPaginate: true, // 翻页功能 bInfo: true, // 页脚信息 bProce ...
- gzip,bzip2,xz压缩工具
gzip,bzip2,xz压缩工具====================== gzip压缩工具 示例:[root@aminglinux yasuo]# ls1.txt 2.txt 3.txt[roo ...
- 【c学习-2】
#include <stdio.h> #define MYVAR 18 int main(){ //float x=3.14; //x=3.14++; //printf("%c\ ...
- js开发中常用小技巧
1.获取指定范围内的随机数 function getRadomNum(min,max){ return Math.floor(Math.random() * (max - min + 1)) + mi ...