POJ 3281 Dining

来源:互联网 发布:jquery form to json 编辑:程序博客网 时间:2024/06/16 19:26

Dining
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 14970 Accepted: 6803
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,F,D三个整数
后面有n行,第i行表示第i头牛,
每行前面两个整数是f与d(当前牛能吃的食物与饮料的种类数量),接着是食物的种类与饮料的种类
求最多头牛的数量,这些牛既能吃到食物又能吃到饮料

建图 源点s - 食物 - 牛 - 牛 - 饮料 - 汇点t
挑战书里的解释

用Dinic邻接矩阵的模板

int mp[500][500];int s, t, N, F, D, n;int dep[500]; //dep[i]表示当前点到起点src的层数int BFS()   // 重新建图(按层数建图){    memset(dep, -1, sizeof(dep));    dep[s] = 0;    queue<int> q;    q.push(s);    while (!q.empty()){        int u = q.front(); q.pop();        for (int v = 1; v <= n; v++){            if (mp[u][v]>0 && dep[v]==-1){   // 如果可以到达且还没有访问                dep[v] = dep[u] + 1;                q.push(v);            }        }    }    if (dep[t] != -1) return 1;    return 0;}int DFS(int u, int mni) // 查找路径上的最小的流量{    if (u == t) return mni;    for (int v = 1; v <= n; v++){        if (mp[u][v]>0 && dep[v]==dep[u]+1){            int d = DFS(v, min(mni, mp[u][v]));            if (d == 0) continue;            mp[u][v] -= d;  //正向减少            mp[v][u] += d;  //反向增加            return d;        }    }    return 0;}int Dinic(){    int ans = 0, d;    while (BFS()){        while (1){            d = DFS(s, inf);            if (!d) break;            ans += d;        }    }    return ans;}int main(void){//  freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin);    int i, j, k, f, d;    while (cin >> N >> F >> D)    {        memset(mp, 0, sizeof(mp));        for (i = 1; i <= N; i++){            scanf("%d %d", &f, &d);            while (f--){                scanf("%d", &k);                mp[k][i+F] = 1;            }            while (d--){                scanf("%d", &k);                mp[i+F+N][k+F+N+N] = 1;            }        }        s = F + N + N + D + 1;        t = s + 1;        for (i = 1; i <= F; i++)            mp[s][i] = 1;        for (i = F+N+N+1; i <= F+N+N+D; i++)            mp[i][t] = 1;        for (i = F+1; i <= F+N; i++)            mp[i][i+N] = 1;        n = t;        printf("%d\n", Dinic());    }    return 0;}
0 0