hdu-4292.food(类dining网络流建图)
Food
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9289 Accepted Submission(s): 3019
The issue comes up as people in your college are more and more difficult to serve with meal: They eat only some certain kinds of food and drink, and with requirement unsatisfied, go away directly.
You have prepared F (1 <= F <= 200) kinds of food and D (1 <= D <= 200) kinds of drink. Each kind of food or drink has certain amount, that is, how many people could this food or drink serve. Besides, You know there’re N (1 <= N <= 200) people and you too can tell people’s personal preference for food and drink.
Back to your goal: to serve as many people as possible. So you must decide a plan where some people are served while requirements of the rest of them are unmet. You should notice that, when one’s requirement is unmet, he/she would just go away, refusing any service.
For each test case, the first line contains three numbers: N,F,D, denoting the number of people, food, and drink.
The second line contains F integers, the ith number of which denotes amount of representative food.
The third line contains D integers, the ith number of which denotes amount of representative drink.
Following is N line, each consisting of a string of length F. e jth character in the ith one of these lines denotes whether people i would accept food j. “Y” for yes and “N” for no.
Following is N line, each consisting of a string of length D. e jth character in the ith one of these lines denotes whether people i would accept drink j. “Y” for yes and “N” for no.
Please process until EOF (End Of File).
1 1 1
1 1 1
YYN
NYY
YNY
YNY
YNY
YYN
YYN
NNY
/*
结点0 ~ n - 1存左牛结点
结点n ~ 2 * n - 1存右牛结点
结点2 * n ~ 2 * n + f - 1存左食物
结点2 * n + f ~ 2 * n + f * 2 - 1存右食物
结点2 * n + 2 * f ~ 2 * n + 2 * f + d - 1存饮料左
结点2 * n + 2 * f + d ~ 2 * n + 2 * f + d * 2 - 1存饮料右
结点2 * n + 2 * f + 2 * d为s,t = s = 1。
*/ #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + , maxm = * + , inf = 0x3f3f3f3f;
int numf[maxn], numd[maxn];
char str[maxn]; int tot, head[maxn << ], que[maxn << ], dep[maxn << ], cur[maxn << ], sta[maxn << ]; struct Edge {
int to, cap, flow, next, from;
} edge[maxm << ]; void init() {
tot = ;
memset(head, -, sizeof head);
} void addedge(int u, int v, int w) {
edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = ; edge[tot].from = u;
edge[tot].next = head[u]; head[u] = tot ++;
edge[tot].to = u; edge[tot].cap = ; edge[tot].flow = ; edge[tot].from = v;
edge[tot].next = head[v]; head[v] = tot ++;
} bool bfs(int s, int t, int n) {
memset(dep, -, sizeof dep[] * (n + ));
int front = , tail = ;
dep[s] = ;
que[tail ++] = s;
while(front < tail) {
int u = que[front ++];
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if(edge[i].cap > edge[i].flow && dep[v] == -) {
dep[v] = dep[u] + ;
if(v == t) return true;
que[tail ++] = v;
}
}
}
return false;
} int dinic(int s, int t, int n) {
int maxflow = ;
while(bfs(s, t, n)) {
for(int i = ; i <= n; i ++) cur[i] = head[i];
int u = s, tail = ;
while(~cur[s]) {
if(u == t) {
int tp = inf;
for(int i = tail - ; i >= ; i --)
tp = min(tp, edge[sta[i]].cap - edge[sta[i]].flow);
maxflow += tp;
for(int i = tail - ; i >= ; i --) {
edge[sta[i]].flow += tp;
edge[sta[i] ^ ].flow -= tp;
if(edge[sta[i]].cap - edge[sta[i]].flow == ) tail = i;
}
u = edge[sta[tail] ^ ].to;
} else if(~ cur[u] && edge[cur[u]].cap > edge[cur[u]].flow && dep[u] + == dep[edge[cur[u]].to]) {
sta[tail ++] = cur[u];
u = edge[cur[u]].to;
} else {
while(u != s && cur[u] == -)
u = edge[sta[-- tail] ^ ].to;
cur[u] = edge[cur[u]].next;
}
}
}
return maxflow;
} int main() {
int n, f, d;
while(~scanf("%d %d %d", &n, &f, &d)) {
init();
int s = * n + * f + d * , t = s + ;//超级源点和超级汇点
for(int i = ; i < f; i ++) {
scanf("%d", &numf[i]);
addedge(s, * n + i, inf);//超级源点到食物左
addedge( * n + i, * n + f + i, numf[i]);//食物左到食物右
}
for(int i = ; i < d; i ++) {
scanf("%d", &numd[i]);
addedge( * n + * f + i, * n + * f + d + i, numd[i]);//饮料左到饮料右
addedge( * n + * f + d + i, t, inf);//饮料右到超级汇点
}
for(int i = ; i < n; i ++) {
addedge(i, n + i, );//左牛到右牛
}
for(int i = ; i < n; i++) {
scanf("%s", str);
for(int j = ; j < f; j ++) {
if(str[j] == 'Y')
addedge( * n + f + j, i, );//从食物右到左牛
}
}
for(int i = ; i < n; i ++) {
scanf("%s", str);
for(int j = ; j < d; j ++) {
if(str[j] == 'Y')
addedge(n + i, * n + * f + j, );//从右牛到左饮料
}
}
// for(int i = 2; i <= tot; i ++) {
// printf("%d -> %d\n", edge[i].from, edge[i].to);
// }
printf("%d\n", dinic(s, t, * n + * f + * d + ));
}
return ;
}
下面这种建图方法和上面的类似,只是上图是拆点限制点流量,而我们知道对于每一件食物,如果我们有一个人选取它,那么它必定是只选取了一件,因为后面拆点n决定的,那么也就是每个人只能取他所喜欢食物中的一种中的一个,所以我们只需要对我们能够提供的某种食物限量就可以了,也就是从源点到某种食物权值为food_num,这样就可以限制住每种食物的用量了,接着看饮料,如果某个人选取了一个饮料,那么他也只能选取这一种饮料中的一瓶,因为前面已经对n拆点导致它能扩展的流也只有1,所以导致她选的饮料也是1对1,所以想要限制饮料的个数,也只需要无限索取,直到最后无法流到汇点就ok,那也就是从饮料到汇点权值为drink_num。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + , maxm = * + , inf = 0x3f3f3f3f;
int numf[maxn], numd[maxn];
char str[maxn]; int tot, head[maxn << ], que[maxn << ], dep[maxn << ], cur[maxn << ], sta[maxn << ]; struct Edge {
int to, cap, flow, next, from;
} edge[maxm << ]; void init() {
tot = ;
memset(head, -, sizeof head);
} void addedge(int u, int v, int w) {
edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = ; edge[tot].from = u;
edge[tot].next = head[u]; head[u] = tot ++;
edge[tot].to = u; edge[tot].cap = ; edge[tot].flow = ; edge[tot].from = v;
edge[tot].next = head[v]; head[v] = tot ++;
} bool bfs(int s, int t, int n) {
memset(dep, -, sizeof dep[] * (n + ));
int front = , tail = ;
dep[s] = ;
que[tail ++] = s;
while(front < tail) {
int u = que[front ++];
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if(edge[i].cap > edge[i].flow && dep[v] == -) {
dep[v] = dep[u] + ;
if(v == t) return true;
que[tail ++] = v;
}
}
}
return false;
} int dinic(int s, int t, int n) {
int maxflow = ;
while(bfs(s, t, n)) {
for(int i = ; i <= n; i ++) cur[i] = head[i];
int u = s, tail = ;
while(~cur[s]) {
if(u == t) {
int tp = inf;
for(int i = tail - ; i >= ; i --)
tp = min(tp, edge[sta[i]].cap - edge[sta[i]].flow);
maxflow += tp;
for(int i = tail - ; i >= ; i --) {
edge[sta[i]].flow += tp;
edge[sta[i] ^ ].flow -= tp;
if(edge[sta[i]].cap - edge[sta[i]].flow == ) tail = i;
}
u = edge[sta[tail] ^ ].to;
} else if(~ cur[u] && edge[cur[u]].cap > edge[cur[u]].flow && dep[u] + == dep[edge[cur[u]].to]) {
sta[tail ++] = cur[u];
u = edge[cur[u]].to;
} else {
while(u != s && cur[u] == -)
u = edge[sta[-- tail] ^ ].to;
cur[u] = edge[cur[u]].next;
}
}
}
return maxflow;
} int main() {
int n, f, d;
while(~scanf("%d %d %d", &n, &f, &d)) {
init();
int s = * n + f + d, t = s + ;//超级源点和超级汇点
for(int i = ; i < f; i ++) {
scanf("%d", &numf[i]);
addedge(s, n * + i, numf[i]);
}
for(int i = ; i < d; i ++) {
scanf("%d", &numd[i]);
addedge(n * + f + i, t, numd[i]);
}
for(int i = ; i < n; i++) {
addedge(i, n + i, );//左牛到右牛
scanf("%s", str);
for(int j = ; j < f; j ++) {
if(str[j] == 'Y')
addedge( * n + j, i, );
}
}
for(int i = ; i < n; i ++) {
scanf("%s", str);
for(int j = ; j < d; j ++) {
if(str[j] == 'Y')
addedge(n + i, * n + f + j, );//从右牛到饮料
}
}
// for(int i = 2; i <= tot; i ++) {
// printf("%d -> %d\n", edge[i].from, edge[i].to);
// }
printf("%d\n", dinic(s, t, * n + f + d + ));
}
return ;
}
不得不承认这种做法确实节省了很多空间呀。
hdu-4292.food(类dining网络流建图)的更多相关文章
- HDU 4888 Redraw Beautiful Drawings 网络流 建图
题意: 给定n, m, k 以下n个整数 a[n] 以下m个整数 b[n] 用数字[0,k]构造一个n*m的矩阵 若有唯一解则输出这个矩阵.若有多解输出Not Unique,若无解输出Impossib ...
- hdu 5294 Tricks Device 最短路建图+最小割
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294 Tricks Device Time Limit: 2000/1000 MS (Java/Other ...
- joj 2453 candy 网络流建图的题
Problem D: Candy As a teacher of a kindergarten, you have many things to do during a day, one of whi ...
- [ZJOI2010]贪吃的老鼠(网络流+建图)
题目描述 奶酪店里最近出现了m只老鼠!它们的目标就是把生产出来的所有奶酪都吃掉.奶酪店中一天会生产n块奶酪,其中第i块的大小为pi,会在第ri秒被生产出来,并且必须在第di秒之前将它吃掉.第j只老鼠吃 ...
- hdu 2647 (拓扑排序 邻接表建图的模板) Reward
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2647 老板给员工发工资,每个人的基本工资都是888,然后还有奖金,然后员工之间有矛盾,有的员工希望比某员 ...
- 网络流建图(典型)(EK)
题目链接:https://cn.vjudge.net/contest/68128#problem/B 具体思路: 按照 源点 - > 食物 - > 牛1 - > 牛2 - > ...
- hdu 4857 逃生 拓扑排序+逆向建图
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Descr ...
- HDU 4444:Walk(思维建图+BFS)***
http://acm.hdu.edu.cn/showproblem.php?pid=4444 题意:给出一个起点一个终点,给出n个矩形的两个对立顶点,问最少需要拐多少次弯可以从起点到达终点,如果不能输 ...
- URAL 1736 Chinese Hockey 网络流+建图
题目链接:点击打开链接 题意: 给定n个队伍的得分情况,输出随意一个可行解. n个队伍随意2个队伍 a, b 间有且仅有一场比赛. 比赛结果分4种: 1.a +3, b +0 2.a +0, b +3 ...
随机推荐
- java并发学习--第十章 java内存模型的内存语义
一.锁的内存语义 所为的java内存模型的内存语义指的就是在JVM中的实现原则. 锁的内存语义:锁除了让临界区互斥执行外,还可以让释放锁的线程向获取同一个锁的线程发送消息. 我们把上面这句话再整理下: ...
- linux 时区问题
1.java项目发现 服务器时间不正确,修改了服务器时间之后依然没解决. 2.java虚拟机的时区也需要设置:
- bzoj2802 [Poi2012]Warehouse Store 贪心+堆
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2802 题解 我一开始想到了一个比较麻烦的做法. 把每一天按照 \(b_i\) 从小到大排序,\ ...
- 前端每日实战:122# 视频演示如何用纯 CSS 创作一个苹果系统的相册图标
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/zJKwbO 可交互视频 此视频是可 ...
- HDU-4280-Island Transport(网络流,最大流, ISAP)
链接: https://vjudge.net/problem/HDU-4280 题意: In the vast waters far far away, there are many islands. ...
- 用idea搭建SSM框架maven项目
打开idea,点击New - Project 找到Maven一栏,因为要搭建的SpringMvc项目,所以选择webapp模板 填写好GroupId和ArtifactId后,一步步next,最后fin ...
- 【bzoj3195】【 [Jxoi2012]奇怪的道路】另类压缩的状压dp好题
(上不了p站我要死了) 啊啊,其实想清楚了还是挺简单的. Description 小宇从历史书上了解到一个古老的文明.这个文明在各个方面高度发达,交通方面也不例外.考古学家已经知道,这个文明在全盛时期 ...
- [CSP-S模拟测试]:毛二琛(DP)
题目描述 $MYC$在$NOI2018$中,遇到了$day1T2$这样一个题,题目是让你求有多少“好”的排列.$MYC$此题没有获得高分,感到非常惭愧,于是回去专心研究排列了.如今数排列的题对$MYC ...
- 图片上传预览转压缩并转base64详解(dShowImg64.js)
hello,大家好,游戏开始了,欢迎大家收看这一期的讲解.本次的内容是图片的上传预览.最后发源码链接.废话不多说,先上图. 待上传图像 点击蓝色框内,pc可以选择文件,移动端选择拍照或选择图片进行上传 ...
- React 进阶设计与控制权问题
控制权--这个概念在编程中至关重要.比如,"轮子"封装层与业务消费层对于控制权的"争夺",就是一个很有意思的话题.这在 React 世界里也不例外.表面上看,我 ...