poj 3281 Dining
来源:互联网 发布:如何摆脱抑郁症知乎 编辑:程序博客网 时间:2024/06/18 07:00
题目:
Description
Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.
Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows as possible.
Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared D (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has decided whether she is willing to eat a particular food or drink a particular drink. Farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both.
Each dish or drink can only be consumed by one cow (i.e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2).
Input
Lines 2..N+1: Each line i starts with a two integers Fi and Di, the number of dishes that cow i likes and the number of drinks that cow i likes. The next Fi integers denote the dishes that cow i will eat, and the Di integers following that denote the drinks that cow i will drink.
Output
Sample Input
4 3 32 2 1 2 3 12 2 2 3 1 22 2 1 3 1 22 1 1 3 3
Sample Output
3
Hint
Cow 1: no meal
Cow 2: Food #2, Drink #2
Cow 3: Food #1, Drink #1
Cow 4: Food #3, Drink #3
The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.
#include<iostream>#include<stdio.h>#include<queue>using namespace std;#define MAXN 500int map[MAXN][MAXN];int flow[MAXN][MAXN];int min_flow[MAXN];int pre[MAXN];bool bfs(int m, int s, int t){memset(min_flow, 0, sizeof(min_flow));min_flow[s] = INT_MAX;queue<int> q;q.push(s);while (!q.empty()){int u = q.front();q.pop();for (int v = s; v < m; ++v){if (min_flow[v] == 0 && map[u][v] > flow[u][v]){pre[v] = u;q.push(v);min_flow[v] = min(min_flow[u], map[u][v] - flow[u][v]);}}}if (min_flow[t] == 0)return false;return true;}int max_flow(int m, int s, int t){int res = 0;memset(pre, 0, sizeof(pre));memset(flow, 0, sizeof(flow));while (bfs(m, s, t)){for (int u = t; u != s; u = pre[u]){flow[pre[u]][u] += min_flow[t];flow[u][pre[u]] -= min_flow[t];}res += min_flow[t];}return res;}int main(){int n, f, d;while (scanf_s("%d%d%d", &n, &f, &d) != EOF){memset(map, 0, sizeof(map));for (int i = 1; i <= f; i++)map[0][i] = 1; //源点-->foodfor (int i = 1; i <= d; i++)map[i + 2*n + f][2*n + f + d + 1] = 1; //drink-->汇点for (int i = 1; i <= n; i++)map[f + i][f + n + i] = 1; //牛指向牛for (int i = 1; i <= n; ++i){int t1, t2;cin >> t1 >> t2;while (t1--){int food;cin >> food;map[food][f + i] = 1;}while (t2--){int drink;cin >> drink;map[f + i + n][2 * n + f + drink] = 1;}}int res = max_flow(f+2*n+d+2, 0, f+2*n+d+1);cout << res << endl;}return 0;}
- poj 3281 Dining Maxflow
- POJ 3281 Dining
- poj 3281 Dining //SAP
- POJ 3281 Dining
- poj 3281 Dining
- poj 3281 Dining
- POJ 3281 Dining
- POJ 3281 Dining
- poj 3281 Dining
- POJ-3281-Dining
- poj(3281)Dining
- POJ 3281 Dining
- poj 3281 Dining
- poj 3281 Dining
- POJ 3281 Dining
- POJ 3281 Dining
- POJ 3281 Dining dinic
- POJ 3281 — Dining
- Linux上如何查看Deb和RPM软件包的更新日志
- MFC DirectUI界面库使用方法
- MySql学习笔记
- 磁盘管理
- 读《DOOM启示录》后之摘录
- poj 3281 Dining
- Reverse Linked List II
- 分布式服务框架 Zookeeper — 管理分布式环境中的数据
- 使用Harbor搭建Docker私有镜像仓库服务
- On-policy Sarsa算法与Off-policy Q learning对比
- spring-framework 下载
- 退出应用的方法
- 0012、node 之简单筛选后台数据以及需要分页时的一种处理方式
- HZAU_1209_Deadline(贪心)