POJ1149 PIGS 【最大流 + 构图】
题目链接:http://poj.org/problem?id=1149
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions:24094 | Accepted: 10982 |
Description
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 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
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int MAXM = ; //猪圈数目上界
const int MAXN = ; //顾客数目上界
const int inf = 0x3f3f3f3f; int m, n; //猪棚数目 顾客数目
int num[MAXM], first[MAXM], vis[MAXM];//每个猪圈里猪的数目 每个猪圈第一个顾客 每个猪圈是否已经有第一个顾客买了
int head[MAXN], cnt;
int dep[MAXN];
queue<int> Q; struct Edge
{
int to, next, flow;
}edge[ * MAXN + * MAXN * MAXN]; void add(int a, int b, int c)
{
cnt ++;
edge[cnt].to = b;
edge[cnt].flow = c;
edge[cnt].next = head[a];
head[a] = cnt;
} int bfs(int st, int ed)
{
if(st == ed)
return ;
while(!Q.empty()) Q.pop();
mem(dep, -);
dep[st] = ;
Q.push(st);
while(!Q.empty())
{
int index = Q.front();
Q.pop();
for(int i = head[index]; i != -; i = edge[i].next)
{
int to = edge[i].to;
if(edge[i].flow > && dep[to] == -)
{
dep[to] = dep[index] + ;
Q.push(to);
}
}
}
return dep[ed] != -;
} int dfs(int now, int ed, int zx)
{
if(now == ed)
return zx;
for(int i = head[now]; i != -; i = edge[i].next)
{
int to = edge[i].to;
if(dep[to] == dep[now] + && edge[i].flow > )
{
int flow = dfs(to, ed, min(zx, edge[i].flow));
if(flow > )
{
edge[i].flow -= flow;
edge[i ^ ].flow += flow;
return flow;
}
}
}
return -;
} void dinic(int st, int ed)
{
int ans = ;
while(bfs(st, ed))
{
while()
{
int inc = dfs(st, ed, inf);
if(inc == -)
break;
ans += inc;
}
}
printf("%d\n", ans);
} int main()
{
int m, n;
scanf("%d%d", &m, &n);
int st = , ed = n + ;
mem(head, -), cnt = -;
mem(first, -), mem(vis, );
for(int i = ; i <= m; i ++)
scanf("%d", &num[i]);
for(int i = ; i <= n; i ++)
{
int k;
scanf("%d", &k);
for(int j = ; j <= k; j ++)
{
int x;
scanf("%d", &x);//猪圈编号
if(!vis[x])
{
first[x] = i;
vis[x] = ;
add(st, i, num[x]);
add(i, st, );
}
else
{
add(first[x], i, inf);
add(i, first[x], );
}
}
int x;
scanf("%d", &x);
add(i, ed, x);
add(ed, i, );
}
dinic(st, ed);
return ;
}
POJ1149
POJ1149 PIGS 【最大流 + 构图】的更多相关文章
- POJ 1149 - PIGS - [最大流构图]
Time Limit: 1000MS Memory Limit: 10000K Description Mirko works on a pig farm that consists of M loc ...
- POJ1149 PIGS [最大流 建图]
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20662 Accepted: 9435 Description ...
- poj1149 PIGS 最大流(神奇的建图)
一开始不看题解,建图出错了.后来发现是题目理解错了. if Mirko wants, he can redistribute the remaining pigs across the unlock ...
- POJ1149 PIGS(最大流)
题意: 有一个人,他有m个猪圈,每个猪圈里面有一定数量的猪,但是每个猪圈的门都是锁着的,他自己没有钥匙,只有顾客有钥匙,一天依次来了n个顾客,(记住是依次来的)他们每个人都有一些钥匙,和他 ...
- 网络流 - 最大流构图入门 bzoj 1305
一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会“单向喜欢”).每个男孩 ...
- 【BZOJ-3638&3272&3267&3502】k-Maximum Subsequence Sum 费用流构图 + 线段树手动增广
3638: Cf172 k-Maximum Subsequence Sum Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 174 Solved: 9 ...
- POJ1149 PIGS
想了好久啊...(#-.-) 开始想到m*n个点的构图,明显超时,于是考虑压缩节点个数 我们发现每个猪圈最后被有且只有一个人调整,于是想到对于一个人,连接他能调整的每个猪圈的上一个控制人.(不懂可以开 ...
- PIGS(最大流)
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18742 Accepted: 8511 Description ...
- POJ1149 PIGS 【最大流量】
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16555 Accepted: 7416 Description ...
随机推荐
- 第九章 利用CSS3制作网页动画
一.CSS3变形transform 1.平移:translate(x,y) translateX(x) translateY(y) 注意:如果想只向X轴平移那么可以translateX,如果想只向X轴 ...
- SIGAI机器学习第七集 k近邻算法
讲授K近邻思想,kNN的预测算法,距离函数,距离度量学习,kNN算法的实际应用. KNN是有监督机器学习算法,K-means是一个聚类算法,都依赖于距离函数.没有训练过程,只有预测过程. 大纲: k近 ...
- click([[data],fn]) 触发每一个匹配元素的click事件。
click([[data],fn]) 概述 触发每一个匹配元素的click事件. 这个函数会调用执行绑定到click事件的所有函数.大理石平台精度等级 参数 fnFunctionV1.0 在每一个匹配 ...
- tarjan缩点——在农场万圣节Trick or Treat on the Farm
一个房间能到另一个房间,有向图,奶牛从自己编号(1到n)的点出发,如果回到以前到过的点就停止,问每头奶牛可以经过几个点: 情况分两种, 一,奶牛在环上,能走的是环的大小,二,一条链连接一个环,大小是链 ...
- 我好菜系列——map查找
链接:https://ac.nowcoder.com/acm/contest/931/A来源:牛客网 DNA序列里只有ACGT四种字母,A和T对应,C和G对应. 俩序列完全对应,就是指它们每一位上的字 ...
- C语言指针方法对字符串进行去重
自己编写了3种方法,都是使用指针的.(在LR中编写的) 1.先在原字符串进行比较,然后再放入目标字符串 Action() { char *srt="aadfeedeewwffggecccew ...
- vue draggable 火狐拖拽搜索问题
最近在使用vuedraggable做导航时候,谷歌拖拽是没问题的,但是在火狐测试时候,拖拽时候是可以成功,但是火狐还是打开了一个新的tab,并且搜索了,一开始想着是阻止默认行为,但是在@end时间中阻 ...
- Mac OS 下三种修改Hosts文件的方法
一.系统偏好设置修改 1.打开系统偏好设置,底部有一个Hosts的快捷入口2.输入ip和hostname后,回车确定,勾选改host即可 二.终端命令行修改 sudo vi /etc/hosts ...
- 使 nodejs 代码 在后端运行(forever)
情境 运行nodejs的程序,使用命令:node xxx.js,但是关掉终端,程序也关闭了,如何让node app的程序一直运行? 解决 1.安装forever npm install -g fore ...
- JDK的新特性
JDK5新特性 a,自动拆装箱 b,泛型 c,可变参数 d,静态导入 e,增强for循环 f,互斥锁 g,枚举 JDK7新特性 * A:二进制字面量 * B:数字字面量可以出现下划线 * C:swit ...