【POJ 3281 Dining】& 网络流Dinic & 拆点

Dining
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 19539 Accepted: 8697
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.

题意: 有多少头牛既能得到自己喜欢的食物又能得到自己喜欢的饮料

思路 : 拆点网络流,源点 s-> 食物 -> 食物一侧的牛 -> 饮料一侧的牛 -> 饮料一侧的牛 -> 汇点 e,每条边的流为1

AC代码 :

#include<cstdio>
#include<cmath>
#include<deque>
#include<queue>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 2e5 + 10;
const int INF = 1e9 + 7;
typedef long long LL;
int head[MAX],d[MAX],nl,lf[110][110],ld[110][110];
void init(){
    nl = 0;
    memset(head,-1,sizeof head);
}
int read(){
    int x = 0, y = 1;
    char s = getchar();
    while(s < '0' || s > '9') { if(s == '-') y = -1;s = getchar();}
    while(s >= '0' && s <= '9') x = x * 10 + s - '0',s = getchar();
    return x * y;
}
struct node{
    int from,to,cap,flow,next;
}st[MAX];
void add(int x,int y,int z){
    st[nl] = node{x,y,z,0,head[x]},head[x] = nl++;
    st[nl] = node{y,x,0,0,head[y]},head[y] = nl++;
}
bool BFS(int s,int e){
    queue <int> q;
    memset(d,-1,sizeof d);
    d[s] = 0,q.push(s);
    while(!q.empty()){
        int o = q.front();
        q.pop();
        for(int i = head[o]; i != -1; i = st[i].next){
            node w = st[i];
            if(d[w.to] == -1 && w.cap - w.flow > 0){
                d[w.to] = d[o] + 1;
                if(w.to == e) return true;
                q.push(w.to);
            }
        }
    }
    return false;
}
int DFS(int s,int x,int e){
    if(s == e || x == 0) return x;
    int flow = 0,o;
    for(int i = head[s]; i != -1; i = st[i].next){
        node& w = st[i];
        if(d[w.to] == d[s] + 1 && (o = DFS(w.to,min(x,w.cap - w.flow),e)) > 0){
            x -= o;
            flow += o;
            w.flow += o;
            st[i^1].flow -= o;
            if(x == 0) break;
        }
    }
    return flow;
}
int Maxflow(int s,int e){
    int flow = 0;
    while(BFS(s,e)){
        flow += DFS(s,INF,e);
    }
    return flow;
}
int main()
{
    int N,F,D;
    N = read(),F = read(),D = read();
    init();
    int s = 0,e = N * 2 + F + D + 1; // 1 ~ N : 食物一侧的1牛
    for(int i = 1; i <= N; i++){    // N + 1 ~ N * 2 : 饮料一侧的牛
        int NF,ND,o;               // 2N + 1 ~ 2N + F : 食物
        scanf("%d %d",&NF,&ND);   // 2N + F + 1 ~ 2F + F + D : 饮料
        for(int j = 1; j <= NF; j++)
            scanf("%d",&o),lf[i][o] = 1;
        for(int j = 1; j <= ND; j++)
            scanf("%d",&o),ld[i][o] = 1;
    }
    for(int i = 1; i <= F; i++) add(s,N * 2 + i,1); // s 和食物连边
    for(int i = 1; i <= D; i++) add(F + 2 * N + i,e,1); // 饮料和 e 连边
    for(int i = 1; i <= N; i++){
        add(i,N + i,1);
        for(int j = 1; j <= F; j++) // 食物和食物一侧的牛连边
            if(lf[i][j]) add(N * 2 + j,i,1);
        for(int j = 1; j <= D ; j++)
            if(ld[i][j]) add(N + i,N * 2 + F + j,1); // 饮料一侧的牛和饮料连边
    }
    printf("%d\n",Maxflow(s,e));
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值