现在的位置: 首页 > 算法 > 正文

poj 3281 最大流

2019年02月28日 算法 ⁄ 共 2791字 ⁄ 字号 评论关闭
Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7589   Accepted: 3472

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.

Source

题意:
有n头牛,f种食物,d种饮料,每种食物只能给1头牛,饮料也是,每头牛有自己喜欢的饮料和食物,问最多能满足多少头牛。
第一行输入是牛的数量,食物数量,饮料数量。
接下来n行先是第i头牛喜欢的食物数量,第i头牛喜欢的饮料数量,后跟对应个数的食物编号,对应个数的饮料编号。
思路:
感觉食物与牛之间像是二分匹配,饮料与牛之间又是二分匹配,如图:
然后把这两个合到一起,加上S和T,就成了这样:
这样看起来就像是最大流问题了,每条线段从左往右流量为1,然而一开始,我就按这样建图写了一遍WA了,之后发现这样建图是不行的,因为可能有多个食物流向牛,牛又流向多个饮料点,得到的结果是大于答案的。于是将图改进了一下:
这样就解决了,将点的序号再整理下,得到:
最终建图完成,从左往右流量是1,0点为源点,15为汇点,求出最大流即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define inf 1<<29
using namespace std;
int p[405][405];
int n,f,d,fnum,dnum,fn,dn,sum;
bool v[405];
int pre[405];
bool bfs(int s,int t)
{
    memset(v,0,sizeof(v));
    queue<int>q;
    q.push(s);
    v[s]=1;
    while(!q.empty())
    {
        int r=q.front();
        q.pop();
        for(int i=0; i<=t; i++)
        {
            if(p[r][i]>0&&v[i]==0)
            {
                pre[i]=r;
                v[i]=1;
                q.push(i);
                if(i==t)return true;
            }
        }
    }
    return false;
}
int ek(int s,int t)
{
    int mf,ans=0;
    while(bfs(s,t))
    {
        mf=inf;
        for(int i=t; i!=s; i=pre[i])mf=mf<p[pre[i]][i]?mf:p[pre[i]][i];
        for(int i=t; i!=s; i=pre[i])
        {
            p[pre[i]][i]-=mf;
            p[i][pre[i]]+=mf;
        }
        ans+=mf;
    }
    return ans;
}
int main()
{
    while(scanf("%d%d%d",&n,&f,&d)!=EOF)
    {
        memset(p,0,sizeof(p));
        sum=2*n+f+d;
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d",&fnum,&dnum);
            for(int j=0; j<fnum; j++)
            {
                scanf("%d",&fn);
                p[fn][f+i]=1;
            }
            for(int j=0; j<dnum; j++)
            {
                scanf("%d",&dn);
                p[f+n+i][f+2*n+dn]=1;
            }
        }
        for(int i=1; i<=f; i++)p[0][i]=1;
        for(int i=1;i<=n;i++)p[f+i][f+n+i]=1;
        for(int i=f+2*n+1; i<=sum; i++)p[i][sum+1]=1;
        cout<<ek(0,sum+1)<<endl;
    }
    return 0;
}

抱歉!评论已关闭.