Dining
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. Source
USACO 2007 Open Gold
|
题意是有N头牛,他们每个有一些喜欢的食物和喜欢的饮料,问你最多有多少头牛可以同时得到自己喜欢的饮料和食物。
思路是建一个超级源点s,一个超级汇点t,s指向所有的食物,食物指向喜欢它的牛,每头牛结点拆成两个,中间有一条边相连,第二列的牛指向他喜欢的饮料,饮料结点都指向t,所有边权都为1,然后求s到t的最大流即可。(图的规模比较小,用Ford-Fulkerson算法)
#include <cstdio> #include <cmath> #include <algorithm> #include <iostream> #include <cstring> #include <map> #include <string> #include <stack> #include <cctype> #include <vector> #include <queue> #include <set> #include <utility> #include <cassert> using namespace std; ///#define Online_Judge #define outstars cout << "***********************" << endl; #define clr(a,b) memset(a,b,sizeof(a)) #define lson l , mid , rt << 1 #define rson mid + 1 , r , rt << 1 | 1 #define mk make_pair #define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++) #define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++) #define REP(i , x , n) for(int i = (x) ; i > (n) ; i--) #define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--) const int MAXN = 120 + 5; const int MAXS = 10000 + 50; const int sigma_size = 26; const long long LLMAX = 0x7fffffffffffffffLL; const long long LLMIN = 0x8000000000000000LL; const int INF = 0x7fffffff; const int IMIN = 0x80000000; const int inf = 1 << 30; #define eps 1e-10 const long long MOD = 1000000000 + 7; const int mod = 10007; typedef long long LL; const double PI = acos(-1.0); //typedef double D; typedef pair<int , int> pii; typedef vector<int> vec; typedef vector<vec> mat; const int MAX_F = 120; const int MAX_D = 120; #define Bug(s) cout << "s = " << s << endl; ///#pragma comment(linker, "/STACK:102400000,102400000"; int N , F , D; bool likeF[MAXN][MAX_F]; bool likeD[MAXN][MAX_D]; struct edge{int to , cap , rev;}; vector<edge>G[MAXN << 2]; bool used[MAXN << 2]; void add_edge(int from , int to , int cap) { G[from].push_back((edge){to , cap , G[to].size()}); G[to].push_back((edge){from , 0 , G[from].size() - 1}); } int dfs(int v , int t , int f) { if(v == t)return f; used[v] = true; for(int i = 0 ; i < G[v].size() ; i++) { edge & e = G[v][i]; if(!used[e.to] && e.cap > 0) { int d = dfs(e.to , t , min(f , e.cap)); if(d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s , int t) { int flow = 0; for(;;) { memset(used , 0 , sizeof(used)); int f = dfs(s , t , INF); if(f == 0)return flow; flow += f; } } void solve() { int s = N *2 + F + D; int t = s + 1; for(int i = 0 ; i < F ; i++) { add_edge(s , N * 2 + i, 1); } for(int i =0 ; i < D ; i++) { add_edge(N * 2 + F +i , t , 1); } for(int i = 0 ; i < N ; i++) { add_edge(i , N + i , 1); for(int j = 0 ; j < F ; j++) { if(likeF[i][j])add_edge(N * 2 + j , i , 1); } for(int j = 0 ; j < D ; j++) { if(likeD[i][j])add_edge(N + i , N * 2 + F + j , 1); } } printf("%d\n" , max_flow(s , t)); } int main() { while(~scanf("%d%d%d" , &N , &F, &D)) { int x , y; clr(likeD , 0); clr(likeF , 0); for(int i = 0 ; i < N ; i++) { scanf("%d%d" , &x , &y); while(x--) { int j; scanf("%d" ,&j); likeF[i][j - 1] = 1; } while(y--) { int j; scanf("%d" , &j); likeD[i][j - 1] = 1; } } solve(); } return 0; }