poj3281 Dining (最大流,建图)

Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 20928   Accepted: 9279

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头牛每头牛,只喜欢几个食物和饮料。
每个食物和饮料只能给一头牛,一头牛只能得到一个食物和饮料,且一头牛必须同时获得一个食物和一个饮料,
问至多有多少头牛可以获得满足。
最大流建图是把食物和饮料放在两端。一头牛拆分成两个点(刚开始没有分成两个点),
分成两个点表示“一头牛必须同时获得一个食物和一个饮料”才能得到满足(流量可以流过),
两点之间的容量为1,喜欢的食物和饮料跟牛建条边,容量为1,
加个源点和汇点。源点与食物、饮料和汇点的边容量都是1,表示每种食物和饮料只有一个。
这样话完全是最大流问题了。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define INF 999999999
using namespace std;
int N,F,D;//N只cows,F种food,D种drinks 
int map[500][500],dis[500];
int T;

int bfs()
{
    memset(dis,-1,sizeof(dis));
    dis[0]=0;
    queue<int> q;
    q.push(0);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=0;i<=T;i++)
        if(dis[i]==-1&&map[u][i]>0)
        {
            dis[i]=dis[u]+1;
            q.push(i);
        }
    }
    if(dis[T]>0) return 1;
    return 0;
}

int dfs(int cur,int m)
{
    if(cur==T) return m;
    int f,res=0;
    for(int i=0;i<=T;i++)
    {
        if(dis[i]==dis[cur]+1&&map[cur][i]>0&&(f=dfs(i,min(m,map[cur][i]))))
        {
            map[cur][i]-=f;
            map[i][cur]+=f;
            res+=f;
            m-=f;
            if(!m) break;
        }
    }
    if(res) return res;
    dis[cur]=-1;
    return 0;
}

int main()
{
    scanf("%d%d%d",&N,&F,&D);
    //0为源点,1到F为food结点,F+1到F+2N为cow结点
    //F+2N+1到F+2N+D为drinks结点,T=F+2N+D+1为汇点
    //一头牛拆成两个点,两个点的边容量为1 
    T=F+N+N+D+1;
    for(int i=1;i<=N;i++)
    {
        int f,d,x;
        scanf("%d%d",&f,&d);
        while(f--)
        {
            scanf("%d",&x);
            map[x][F+i]=1;//food->cow
        }
        while(d--)
        {
            scanf("%d",&x);
            map[F+N+i][F+N+N+x]=1;//cow->drink
        }
    }
    for(int i=1;i<=F;i++) map[0][i]=1;//源点->food 
    for(int i=1;i<=D;i++) map[F+N+N+i][T]=1;//drink->汇点 
    for(int i=1;i<=N;i++) map[F+i][F+i+N]=1;//cow->cow
    
    int ans=0,res;
    while(bfs())
    {
        while(res=dfs(0,INF)) ans+=res;
    }
    printf("%d
",ans);
    return 0;
}
View Code
 
 
 
 
原文地址:https://www.cnblogs.com/ACRykl/p/8784264.html