最大流POJ - 3281

xiaoxiao2021-02-27  495

Dining Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 17498 Accepted: 7789

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

Hint

One way to satisfy three cows is: 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.

题意:有N头牛,F种食物和D种饮料,每头牛要分配到一种食物和一种饮料才满足,一种食物和饮料只能给一头牛,问最多能满足多少头牛。

输入的N行头两个数Fi和Di表示这头牛喜欢的食物种类数和饮料种类数,然后是食物和饮料。

解题:最大流问题,牛要和食物和饮料两种东西联系,就把牛放中间,按源点->食物->牛(左)->牛(右)->饮料->汇点做网络流。

#include <iostream> #include <cstdio> #include <cstring> #include <set> #include <algorithm> #include <queue> using namespace std; int n; int g[5000][500],path[500], flow[500], start, endd; const int INF = 1e9; int bfs() { queue<int> que; memset(path, -1, sizeof(path)); path[start] = 0; flow[start] = INF; // 流进看作无限大 que.push(start); while(!que.empty()) { int t = que.front(); que.pop(); if(t == endd) break; for(int i = 0; i <= n; i++) { if(i!=start && path[i]==-1 && g[t][i]) { flow[i] = min(flow[t], g[t][i]); que.push(i); path[i] = t; } } } if(path[endd] == -1) return -1; return flow[endd]; } int Edmonds_Karp() { int max_flow = 0; int step, now, pre; while(step = bfs() != -1) { max_flow += step; now = endd; while(now != start) { pre = path[now]; g[pre][now] -= step; g[now][pre] += step; now = pre; } } return max_flow; } int main() { int F, N, D; while(~scanf("%d%d%d", &N, &F, &D)) { memset(g, 0, sizeof(g)); n = F+2*N+D+1; start = 0; endd = n; for(int i = 1; i <= F; i++) g[0][i] = 1; // 食物到源点连边 for(int i = F+2*N+1; i <= F+2*N+D; i++) g[i][n] = 1; // 饮料到汇点连边 for(int i = 1; i <= N; i++) g[F+2*i-1][F+2*i] = 1; // 牛拆点连边 int k1, k2, u; for(int i = 1; i <= N; i++) { scanf("%d%d", &k1, &k2); while(k1--) { scanf("%d", &u); g[u][F+2*i-1] = 1; } while(k2--) { scanf("%d", &u); g[F+2*i][F+2*N+u] = 1; } } printf("%d\n", Edmonds_Karp()); } return 0; }

转载请注明原文地址: https://www.6miu.com/read-782.html

最新回复(0)