1. 程式人生 > >POJ 3281 Dining(最大流)

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: 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頭牛來挑食物和飲料,每頭牛隻能挑食物和飲料的各一種,且每種飲料和食物只能被選一次,問最多有多少隻牛可以挑選到食物和飲料?

分析:第一道網路流的題,自己在建圖的時候,是把食物和飲料直接用一條邊連線了起來,後來·WA了幾次,想了想,這樣建圖是不行的,因為如果這樣建圖的話,會使得一頭牛和的飲料不是該牛喜歡的,有可能是其他牛喜歡的。所以在建圖時,用牛來聯絡食物和飲料,這樣就可以滿足要求了。

第一次做網路流的題就把圖建錯了,GG。。。。。

程式碼:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cstdio>
using namespace std;
const int maxn=1e6+500;
const int maxm=2*maxn;
const int inf=0x3f3f3f3f;
int n,f,d;
int FOOD[maxn];
int DRINK[maxn];
int dis[maxn],cur[maxn];
struct Edge
{
    int to,next,cap,flow;
};
struct Graph
{
    int head[maxn],cnt;
    Edge edge[maxm];
    void init()
    {
        cnt=0;
        memset(head,-1,sizeof(head));
    }
    void addedge(int from,int to,int w)
    {
        edge[cnt].to=to;
        edge[cnt].cap=w;
        edge[cnt].flow=0;
        edge[cnt].next=head[from];
        head[from]=cnt++;
    }
}G;
bool bfs(int s,int t)
{
    queue<int> Q;
    memset(dis,-1,sizeof(dis));
    Q.push(s);
    dis[s]=0;
    while(!Q.empty()){
        int now=Q.front();
        Q.pop();
        for(int i=G.head[now];i!=-1;i=G.edge[i].next){
            Edge &e=G.edge[i];
            if(dis[e.to]==-1&&e.cap>e.flow){
                dis[e.to]=dis[now]+1;
                Q.push(e.to);
            }
        }
    }
    return dis[t]!=-1;
}
int dfs(int x,int a,int s,int t)
{
    if(x==t||a==0) return a;
    int flow=0,f;
    for(int &i=cur[x];i!=-1;i=G.edge[i].next){
        Edge &e=G.edge[i];
        if(dis[x]+1==dis[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow),s,t))>0){
            G.edge[i].flow+=f;
            G.edge[i^1].flow-=f;
            flow+=f;
            a-=f;
            if(a==0) break;
        }
    }
    if(flow==0) dis[x]=-2;
    return flow;
}
int Maxflow(int s,int t)
{
    int flow=0;
    while(bfs(s,t)){
        for(int i=0;i<G.cnt;i++) cur[i]=G.head[i];
        flow+=dfs(s,inf,s,t);
    }
    return flow;
}
void add(int from,int to,int w)
{
    G.addedge(from,to,w);
    G.addedge(to,from,0);
}
void CIN()     ///讀入,建圖
{
    for(int i=1; i<=f; i++)
        add(0,i,1);
    for(int i=1; i<=n; i++)
    {
        int F,D;
        scanf("%d%d",&F,&D);
        for(int j=1; j<=F; j++)
            scanf("%d",&FOOD[j]);
        for(int j=1; j<=D; j++)
            scanf("%d",&DRINK[j]);
        for(int j=1; j<=F; j++)
            add(FOOD[j],i+f,1);
        for(int j=1;j<=D;j++)
            add(i+n+f,DRINK[j]+f+2*n,1);
    }
    for(int i=1;i<=n;i++)
        add(i+f,i+n+f,1);
    for(int i=1; i<=d; i++)
    {
        add(f+2*n+i,500,1);
    }
}
int main()
{
    while(scanf("%d%d%d",&n,&f,&d)==3){
        G.init();
        CIN();
        int ss=0;
        int tt=500;
        printf("%d\n",Maxflow(ss,tt));     ///用了同學的dinc模板 QAQ
    }
    return 0;
}