POJ 3281 Dining

来源:互联网 发布:牛顿环和劈尖实验数据 编辑:程序博客网 时间:2024/06/13 10:17

Dining
Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

Submit Status

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 32 2 1 2 3 12 2 2 3 1 22 2 1 3 1 22 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

USACO 2007 Open Gold

思路:

题意是给定一些牛,一些食物,一些饮料,每头牛都有其喜欢的几种食物和几种饮料,求最多能给多少头牛即找到食物又找到饮料~也就是有多少个 牛---食物---饮料 的匹配,而且满足一一匹配,每个牛,食物,或饮料都只能使用一次。网络流的限制是加于边上的,而对于点就没办法了,但是我们可以制造边,也就是把原来的一个点拆成两个,中间连一条边

dinic 大法好

#include <iostream>#pragma comment (linker,"/STACK:102400000,102400000")#include <algorithm>#include <cstdio>#include <queue>#include <cmath>#include <vector>#include <cstring>using namespace std;#define maxn 550#define maxm 2000010#define INF 0x3f3f3f3fint first[maxn],iter[maxn];int next_[maxm];int u[maxm],v[maxm],c[maxm];int flow[maxm];int vis[maxn];int lv[maxn];int e_case;int n,f,d;int map_[maxn][maxn];void add_edge(int _u,int _v,int _w){    int e;    e=e_case++;    u[e]=_u,v[e]=_v,c[e]=_w;    next_[e]=first[u[e]],first[u[e]]=e;    e=e_case++;    u[e]=_v,v[e]=_u,c[e]=0;    next_[e]=first[u[e]],first[u[e]]=e;}void dinic_bfs(int s){    memset(lv,-1,sizeof(lv));    lv[s]=0;    int f,r;    int q[maxn];    q[r=f=0]=s;    while(f<=r)    {        int x=q[f++];        for(int e=first[x];~e;e=next_[e])        {            if(lv[v[e]]<0 && c[e]>flow[e])            {                lv[v[e]]=lv[u[e]]+1;                q[++r]=v[e];            }        }    }}int dinic_dfs(int s,int t,int f){    if(s==t) return f;    for(int &e=iter[s];~e;e=next_[e])    {        if(lv[v[e]]>lv[u[e]] && c[e]>flow[e])        {            int d=dinic_dfs(v[e],t,min(f,c[e]-flow[e]));            if(d>0)            {                flow[e]+=d;                flow[1^e]-=d;                return d;            }        }    }    return 0;}int max_flow(int s,int t){    int total_flow=0;    memset(flow,0,sizeof(flow));    for(;;)    {        dinic_bfs(s);        if(lv[t]<0) return total_flow;        memcpy(iter,first,sizeof(iter));        int f_;        while( (f_=dinic_dfs(s,t,INF))>0 )            total_flow+=f_;    }    return total_flow;}int main(){    while(~scanf("%d%d%d",&n,&f,&d))    {        memset(map_,0,sizeof(map_));        e_case=0;        memset(first,-1,sizeof(first));        for(int i=1;i<=n;i++)            add_edge(i,i+n,1);        for(int i=1;i<=n;i++)        {            int nn,mm;            int a[200];            int b[200];            scanf("%d%d",&nn,&mm);            for(int j=1;j<=nn;j++)                scanf("%d",&a[j]),a[j]+=200;            for(int j=1;j<=mm;j++)                scanf("%d",&b[j]),b[j]+=300;             for(int j=1;j<=nn;j++)                if(!map_[a[j]][i])                add_edge(a[j],i,1),map_[a[j]][i]=1;             for(int j=1;j<=mm;j++)             if(!map_[i][b[j]])                add_edge(i+n,b[j],1),map_[i][b[j]]=1;            for(int j=1;j<=nn;j++)                if(!map_[0][a[j]] )              add_edge(0,a[j],1),map_[0][a[j]]=1;            for(int j=1;j<=mm;j++)               if(!map_[b[j]][500] )                add_edge(b[j],500,1),map_[b[j]][500]=1;        }        cout<<max_flow(0,500)<<endl;    }    return 0;}


1 0
原创粉丝点击