码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3281(构图+网络流)

时间:2016-06-02 21:38:00      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14144   Accepted: 6425

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

 

题意:有N头牛,F种食物,D种饮料,第I头牛喜欢Fi种食物,Di种饮料已知一头牛最多能吃一种食物和一种饮料,每种饮料或食物最多能被一头牛吃,求以上条件下,最多能有多少头牛能吃到他所喜爱的食物和饮料。

构图:S向每种食物连容量1的边,每种食物向喜爱它的牛连容量1的边,将牛拆点,I向I’连容量1的边,I’向它喜爱的饮料连容量1的边,每种饮料向T连容量1的边。最大流。

总结一句话:限制某一个元素的使用次数:拆点之后在其中连一条容量为限制次数的边.

#include <stdio.h>
#include <algorithm>
#include <queue>
#include <string.h>
#include <math.h>
#include <iostream>
#include <math.h>
using namespace std;
typedef long long LL;
const int N = 5005;
const int INF = 999999999;
struct Edge{
    int v,next;
    int w;
}edge[N*N];
int head[N];
int level[N];
int tot;
void init(){
    memset(head,-1,sizeof(head));
    tot=0;
}
void addEdge(int u,int v,int w,int &k){
    edge[k].v = v,edge[k].w=w,edge[k].next=head[u],head[u]=k++;
    edge[k].v = u,edge[k].w=0,edge[k].next=head[v],head[v]=k++;
}
int BFS(int src,int des){
    queue<int >q;
    memset(level,0,sizeof(level));
    level[src]=1;
    q.push(src);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        if(u==des) return 1;
        for(int k = head[u];k!=-1;k=edge[k].next){
            int v = edge[k].v;
            int w = edge[k].w;
            if(level[v]==0&&w!=0){
                level[v]=level[u]+1;
                q.push(v);
            }
        }
    }
    return -1;
}
int dfs(int u,int des,int increaseRoad){
    if(u==des) return increaseRoad;
    int ret=0;
    for(int k=head[u];k!=-1;k=edge[k].next){
        int v = edge[k].v;
        int w = edge[k].w;
        if(level[v]==level[u]+1&&w!=0){
            int MIN = min(increaseRoad-ret,w);
            w = dfs(v,des,MIN);
            edge[k].w -=w;
            edge[k^1].w+=w;
            ret+=w;
            if(ret==increaseRoad) return ret;
        }
    }
    return ret;
}
LL Dinic(int src,int des){
    LL ans = 0;
    while(BFS(src,des)!=-1) ans+=(LL)dfs(src,des,INF*1.0);
    return ans;
}

int main(){
    int n,f,d;
    while(scanf("%d%d%d",&n,&f,&d)!=EOF){
        init();
        int src = 0,des = 2*n+f+d+1;
        for(int i=1;i<=f;i++) addEdge(src,i,1,tot);
        for(int i=1;i<=d;i++) addEdge(2*n+f+i,des,1,tot);
        for(int i=f+1;i<=f+n;i++){
            addEdge(i,i+n,1,tot);
            int a,b,c;
            scanf("%d%d",&a,&b);
            for(int j=1;j<=a;j++){
                scanf("%d",&c);
                addEdge(c,i,1,tot);
            }
            for(int j=1;j<=b;j++){
                scanf("%d",&c);
                addEdge(i+n,2*n+f+c,1,tot);
            }

        }
        printf("%d\n",Dinic(src,des));
    }
}

 

poj 3281(构图+网络流)

标签:

原文地址:http://www.cnblogs.com/liyinggang/p/5554180.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!