Dining
时间限制2秒 内存限制64M
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
Line 1: Three space-separated integers: N , F , and D
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
Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes
Sample Input
4 3 3
2 2 1 2 3 1
2 2 2 3 1 2
2 2 1 3 1 2
2 1 1 3 3
Sample Output
时间限制2秒 内存限制64M
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
Line 1: Three space-separated integers: N , F , and D
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
Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes
Sample Input
4 3 3
2 2 1 2 3 1
2 2 2 3 1 2
2 2 1 3 1 2
2 1 1 3 3
Sample Output
3
solution:
建图是注意牛要拆点,因为牛点上有流量限制(每头牛只能选一份套餐),把牛拆点并将边权赋值为1,建完边后就是裸的求最大流的题
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
#define maxn 250000
#define INF 10000
int head[maxn], cur[maxn], d[maxn], st[maxn * 2], s, t, no;
int n, m;
struct point{
int from, to, flow, next;
}p[maxn * 2];
void add(int u, int v, int z){
p[no].from = u; p[no].to = v; p[no].flow = z; p[no].next = head[u]; head[u] = no++;
p[no].from = v; p[no].to = u; p[no].flow = 0; p[no].next = head[v]; head[v] = no++;
}
void init(){
memset(head, -1, sizeof(head));
no = 0;
}
bool bfs(){
int i, x, y;
queue<int>q;
memset(d, -1, sizeof(d));
d[s] = 0; q.push(s);
while (!q.empty()){
x = q.front(); q.pop();
for (i = head[x]; i != -1; i = p[i].next){
if (p[i].flow && d[y = p[i].to] < 0){
d[y] = d[x] + 1;
if (y == t) return true;
q.push(y);
}
}
}
return false;
}
int dinic(){
int i, loc, top, x = s, nowflow, maxflow = 0;
while (bfs()){
for (i = s; i <= t; i++)cur[i] = head[i];
top = 0;
while (true){
if (x == t){
nowflow = INF;
for (i = 0; i < top; i++){
if (nowflow > p[st[i]].flow){
nowflow = p[st[i]].flow;
loc = i;
}
}
for (i = 0; i < top; i++){
p[st[i]].flow -= nowflow;
p[st[i] ^ 1].flow += nowflow;
}
maxflow += nowflow;
top = loc; x = p[st[top]].from;
}
for (i = cur[x]; i != -1; i = p[i].next)
if (p[i].flow && d[p[i].to] == d[x] + 1) break;
cur[x] = i;
if (i != -1){
st[top++] = i;
x = p[i].to;
}
else {
if (!top) break;
d[x] = -1;
x = p[st[--top]].from;
}
}
}
return maxflow;
}
int main()
{
int i, j, cost, u, v;
int n, f, d;
scanf("%d%d%d", &n, &f, &d);
s = 0; t = f + 2 * n + d + 1;
init();
for (i = 1; i <= f; i++)
add(s, i, 1);
for (i = 1; i <= d; i++)
add(f + 2 * n + i, t, 1);
for (i = 1; i <= n; i++)
add(f + i, f + n + i, 1);
for (i = 1; i <= n; i++)
{
int ff, dd;
scanf("%d%d", &ff, &dd);
for (int j = 1; j <= ff; j++)
{
scanf("%d", &cost);
add(cost, f + i, 1);
}
for (int j = 1; j <= dd; j++)
{
scanf("%d", &cost);
add(f + n + i, f + 2 * n + cost, 1);
}
}
printf("%d\n", dinic());
return 0;
}