POJ 3281 Dining(转换+最大流)

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  Fiintegers 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行,告诉你每天牛想吃的食物和饮料,一头牛吃到食物和饮料就OK,问你最多使多少头牛OK。


这题需要转换一下,可以看出食物--牛--饮料,这样处理还是很麻烦,所以可以看出两个部分食物--牛,牛--饮料,然后牛--牛链接一下就可以了,最后把源点和汇点加上去,即源点--食物--牛--牛--饮料--汇点,然后将食物牛饮料编号跑最大流模板就可以了。


源点编号0

牛的编号1--2*n

食物的编号2*n+1--2*n+m

饮料的编号2*n+m+1--2*n+m+k

汇点的编号2*n+m+k+1


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define N 410
#define inf 1000000000
queue<int> q;
int s[N][N],a[N],b[N];
int bfs(int x,int y)
{
    while(!q.empty())
        q.pop();
    int i,j,k;
    memset(a,-1,sizeof(a));
    a[x]=0;
    b[x]=inf;
    q.push(x);
    while(!q.empty())
    {
        k=q.front();
        q.pop();
        if(k==y)
            break;
        for(i=1;i<=y;i++)
        {
            if(i!=x&&a[i]==-1&&s[k][i])
            {
                b[i]=min(b[k],s[k][i]);
                q.push(i);
                a[i]=k;
            }
        }
    }
    if(a[y]==-1)
        return 0;
    return b[y];
}
int solve(int x,int y)
{
    int ans=0,i,j,k;
    while(i=bfs(x,y))
    {
        ans=ans+i;
        k=y;
        while(k!=x)
        {
            j=a[k];
            s[j][k]-=i;
            s[k][j]+=i;
            k=j;
        }
    }
    return ans;
}
int main()
{
    int n,m,i,j,k,x,y,z;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        memset(s,0,sizeof(s));
        for(i=1;i<=m;i++)
            s[0][i+2*n]+=1;//源点到食物
        for(i=1;i<=k;i++)
            s[i+2*n+m][2*n+m+k+1]+=1;//饮料到汇点
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&x,&y);
            while(x--)
            {
                scanf("%d",&z);
                s[z+2*n][i]+=1;
            }
            while(y--)
            {
                scanf("%d",&z);
                s[i+n][z+2*n+m]+=1;
            }
            s[i][i+n]+=1;
        }
        printf("%d\n",solve(0,2*n+m+k+1));
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_37751662/article/details/78806828