POJ-3281 Dining(最大流)

来源:互联网 发布:adidas跑鞋系列 知乎 编辑:程序博客网 时间:2024/06/08 02:51

Dining
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 15653 Accepted: 7085

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: NF, 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 32 2 1 2 3 12 2 2 3 1 22 2 1 3 1 22 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.

一开始直接把食物放在左边,牛放在中间,饮料放在右边,果断WA了,想了很久没想出原因,画了下图后顿悟= =

一定要把牛拆点,才能保证每头牛只吃一种食物喝一种饮料



题意:有N头牛,F种食物,D种饮料,每头牛都有FI种喜欢的食物,Di种喜欢的饮料,每种食物和饮料只能被使用一次,每头牛都想吃一种食物并且喝一种饮料,问最多能满足多少头牛

题解:最大流拆点,把牛的点拆开,用一条容量为1的边连起来,对于每头牛,把每个喜欢吃的食物与牛连起来,在把喜欢喝的饮料与牛连起来,最后把所有的食物与源点连起来,把所有饮料与汇点连起来,跑一遍最大流,最大流就是最多能满足的牛的数目

#include<cstdio>#include<algorithm>#include<string.h>#include<queue>using namespace std;const int maxn = 405;const int maxe = 4*maxn*maxn;const int inf = 0x3f3f3f3f;struct MaxFlow {    struct Edge {        int v, w, nxt;    } edge[maxe];    int head[maxn], tot, level[maxn];    void init(){        memset(head,-1,sizeof(head));        tot=0;    }    void add(int u, int v, int w) {        edge[tot].v = v;        edge[tot].w = w;        edge[tot].nxt = head[u];        head[u] = tot++;        edge[tot].v = u;        edge[tot].w = 0;        edge[tot].nxt = head[v];        head[v] = tot++;    }    bool bfs(int s, int t) {        memset(level, -1, sizeof(level));        queue<int>q;        q.push(s);        level[s] = 0;        while(!q.empty()) {            int u = q.front(); q.pop();            for(int i = head[u]; ~i; i = edge[i].nxt) {                if(edge[i].w > 0 && level[edge[i].v] < 0) {                    level[edge[i].v] = level[u] + 1;                    q.push(edge[i].v);                }            }        }        return level[t] > 0;    }    int dfs(int u, int t, int f) {        if(u == t) return f;        for(int i = head[u]; ~i; i = edge[i].nxt) {            int v = edge[i].v;            if(edge[i].w > 0 && level[v] > level[u]) {                int d = dfs(v, t, min(f, edge[i].w));                if(d > 0) {                    edge[i].w -= d;                    edge[i ^ 1].w += d;                    return d;                }            }        }        level[u] = -1; //不太确定,如果WA了把这句删掉试试        return 0;    }    int solve(int s, int t) {        int flow = 0, f;        while(bfs(s, t)) {            while(f = dfs(s, t, inf)) flow += f;        }        return flow;    }}F;int main(){    int n,f,d;   // freopen("in.txt","r",stdin);    while(~scanf("%d%d%d",&n,&f,&d)){        F.init();        for(int i=1;i<=n;i++){            int t1,t2,v;            scanf("%d%d",&t1,&t2);            for(int j=1;j<=t1;j++){                scanf("%d",&v);                F.add(v,i+f,1);            }            for(int j=1;j<=t2;j++){                scanf("%d",&v);                F.add(i+n+f,v+2*n+f,1);            }        }        for(int i=1;i<=n;i++) F.add(i+f,i+f+n,1);        for(int i=1;i<=f;i++) F.add(0,i,1);        for(int i=1;i<=d;i++) F.add(i+2*n+f,2*n+f+d+1,1);        printf("%d\n",F.solve(0,2*n+f+d+1));    }    return 0;}


0 0