1. 程式人生 > >B-Dining(EK演算法)

B-Dining(EK演算法)

B - 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 ilikes. 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.

 

思路:

        EK演算法,主要在建圖

EK演算法參考部落格:

http://www.cnblogs.com/kuangbin/archive/2011/07/26/2117636.html

如此建圖 一直不過 的 程式碼:

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
const int MM=510;
const int INF=0x3f3f3f3f;
int tu[MM][MM];
int path[MM],flow[MM],st,en,n,f,d;
queue<int>qu;
int bfs()
{
    while(!qu.empty())
        qu.pop();
    mem(path,-1);
    mem(flow,INF);

    path[st]=0;
    qu.push(st);
    while(!qu.empty())
    {
        int kk=qu.front();
        qu.pop();
        if(kk==en)
            break;
        for(int i=1; i<=en; i++)
        {
            if(tu[kk][i] && path[i]==-1 && i!=st)
            {
                flow[i]=min(flow[kk],tu[kk][i]);
                qu.push(i);
                path[i]=kk;
            }
        }
    }
    if(path[en]==-1)
        return -1;
    return flow[en];
}
int EK()
{
    int now,pre,maxx=0,step;
    while((step=bfs())!=-1)
    {
        maxx+=step;
        now=en;
        while(now!=st)
        {
            pre=path[now];
            tu[pre][now]-=step;
            tu[now][pre]+=step;
            now=pre;
        }
    }
    return maxx;
}
int main()
{
     while(~scanf("%d%d%d",&n,&f,&d))
     {

        mem(tu,0);
        int i,j;
        int F,D,ff,dd;
        st=1;
        en=1+n*2+f+d+1;
       // printf("st=%d en=%d\n",st,en);
        for(i=1; i<=f; i++)
            tu[1][i+1]=1;
        for(i=1; i<=d; i++)
            tu[en-i][en]=1;
        for(i=1; i<=n; i++)
        {
            tu[i+f+1][i+f+2]=1;
            scanf("%d%d",&F,&D);
            for(j=1; j<=F; j++)
            {
                scanf("%d",&ff);
                tu[ff+1][i+f+1]=1;
            }
            for(j=1;j<=D;j++)
            {
                scanf("%d",&dd);
                tu[i+f+2][en-(d-dd+1)]=1;
            }
        }
        printf("%d\n",EK());
     }
    return 0;
}

 

建圖 一發 的程式碼: 

int main()
{
    while(~scanf("%d%d%d",&n,&f,&d))
    {
        for(int i=1;i<=f;i++)
        tu[0][2*n+i]=1;
        for(int i=1;i<=d;i++)
        tu[2*n+f+i][2*n+f+d+1]=1;
        for(int i=1;i<=n;i++)
        {
            tu[2*i-1][2*i]=1;
            int a,b;
            scanf("%d%d",&a,&b);
            for(int j=1;j<=a;j++)
            {
                int x;
                scanf("%d",&x);
                tu[2*n+x][2*i-1]=1;
            }
            for(int j=1;j<=b;j++)
            {
                int x;
                scanf("%d",&x);
                tu[2*i][2*n+f+x]=1;
            }
        }
         st=0;
         en=2*n+d+f+1;
        printf("%d\n",EK());
     }
    return 0;
}