Dining POJ

Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 25452   Accepted: 11183

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 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,第2到N+1行每行输入Fi和Di,后面接着Fi个数和Di个数,代表第i头牛能吃Fi种食物,喝Di种饮料,接着的Fi个数代表食物号,Di个数代表饮料号,一种食物和饮料只有一个,问怎么分配可以使最多数量的牛满足它们的要求(即吃到能吃的食物并喝到能喝的饮料)。

题解:建图方法,将牛拆分为两个点,之间建边流量为1。  源点->食物->牛1->牛2->饮料->汇点

ps:下面的dinic一开始没有反向边建0在poj上wa了。。。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
using namespace std;
#define LL long long
#define add addedge
#define infw in
#define ofw out
#define inf INF
#define mp edge
const int MAXN = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const LL mod =  998244353;
int N,F,D;

struct Edge{
    int u,v,cap,next;
}edge[MAXN];
int pre[MAXN];
int dis[MAXN],cur[MAXN];
int cnt;
int sp,tp;

void init(){
    cnt = 0;
    memset(pre,-1, sizeof(pre));
}

void addedge(int u,int v,int w){
    edge[cnt].u = u;
    edge[cnt].v = v;
    edge[cnt].cap = w;
    edge[cnt].next = pre[u];
    pre[u] = cnt++;

    edge[cnt].u = v;
    edge[cnt].v = u;
    edge[cnt].cap = 0;
    edge[cnt].next = pre[v];
    pre[v] = cnt ++;
}

bool bfs(){
    memset(dis,-1,sizeof dis);
    queue<int>que;
    while(!que.empty()) que.pop();
    que.push(sp);
    dis[sp] = 0;
    int u,v;
    while(!que.empty()){
        u = que.front();
        que.pop();
        for(int i = pre[u]; i != -1; i = edge[i].next){
            v = edge[i].v;
            if(dis[v] == -1 && edge[i].cap > 0){
                dis[v] = dis[u] + 1;
                que.push(v);
                if(v == tp)
                    break;
            }
        }
    }
    return dis[tp] != -1;
}

int dfs(int u,int cap){
    if(u == tp || cap == 0) return cap;
    int res = 0,f;
    for(int i = cur[u]; i != -1 ; i = edge[i].next){
        int v = edge[i].v;
        if(dis[v] == dis[u] + 1 && (f = dfs(v,min(cap -  res,edge[i].cap))) > 0){
            edge[i].cap -= f;
            edge[i ^ 1].cap += f;
            res += f;
            if(res == cap)
                return cap;
        }
    }
    if(!res)
        dis[u] = -1;
    return res;
}
int dinic(){
    int ans = 0;
    while(bfs()){
        for(int i = sp ; i <= tp ;i++)
            cur[i] = pre[i];
        ans += dfs(sp,INF);
    }
    return ans;
}
int main()
{
    sp = 0;
    tp = 500;
    init();
    while(~scanf("%d %d %d",&N,&F,&D)) {
        int ff[MAXN], dd[MAXN];
        for (int i = 1; i <= F; i++){
            addedge(sp, i + 100, 1);
            addedge(i + 100,sp,0);
        }
        for (int i = 1; i <= D; i++) {
            addedge(i + 200, tp, 1);
            addedge(tp, i + 200, 0);
        }
        for (int i = 1; i <= N; i++) {
            addedge(i, i + 300, 1);
            addedge(i + 300,i,0);
            int nf, nd;
            scanf("%d %d", &nf, &nd);
            int tmp;
            for (int j = 0; j < nf; j++) {
                scanf("%d", &tmp);
                addedge(tmp + 100, i, 1);
                addedge(i,tmp + 100,0);
            }
            for (int j = 0; j < nd; j++) {
                scanf("%d", &tmp);
                addedge(i + 300, tmp + 200, 1);
                addedge(tmp + 200,i + 300,0);
            }
        }
        int ans = dinic();
        printf("%d
", ans);
    }
}
View Code
原文地址:https://www.cnblogs.com/smallhester/p/11252164.html