首页 > 代码库 > POJ 3281(Dining-网络流拆点)[Template:网络流dinic]

POJ 3281(Dining-网络流拆点)[Template:网络流dinic]

Language:
Dining
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9631 Accepted: 4446

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

USACO 2007 Open Gold


首先s向食物连边,饮料向t连边,容量=1(每份食物只有一份)

然后对应的食物向牛,再向对应的饮料连边,容量=1,表示1种取法

但是一只牛只能取一份,所以牛代表的点本身容量=1,故拆点。


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXn (100+10)
#define MAXf (100+10)
#define MAXd (100+10)
#define MAXN (1000+10)
#define MAXM ((30300)*2+100)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
class Max_flow  //dinic+当前弧优化 
{  
public:  
    int n,s,t;  
    int q[10000];  
    int edge[MAXM],next[MAXM],pre[MAXN],weight[MAXM],size;  
    void addedge(int u,int v,int w)    
    {    
        edge[++size]=v;    
        weight[size]=w;    
        next[size]=pre[u];    
        pre[u]=size;    
    }    
    void addedge2(int u,int v,int w){addedge(u,v,w),addedge(v,u,0);}   
    bool b[MAXN];  
    int d[MAXN];  
    bool SPFA(int s,int t)    
    {    
        For(i,n) d[i]=INF;  
        MEM(b)  
        d[q[1]=s]=0;b[s]=1;    
        int head=1,tail=1;    
        while (head<=tail)    
        {    
            int now=q[head++];    
            Forp(now)    
            {    
                int &v=edge[p];    
                if (weight[p]&&!b[v])    
                {    
                    d[v]=d[now]+1;    
                    b[v]=1,q[++tail]=v;    
                }    
            }        
        }    
        return b[t];    
    }   
    int iter[MAXN];
    int dfs(int x,int f)
	{
		if (x==t) return f;
		Forpiter(x)
		{
			int v=edge[p];
			if (weight[p]&&d[x]<d[v])
			{
				  int nowflow=dfs(v,min(weight[p],f));
				  if (nowflow)
				  {
				  	weight[p]-=nowflow;
				  	weight[p^1]+=nowflow;
				  	return nowflow;
				  }
			}
		}
		return 0;
	}
	int max_flow(int s,int t)
	{
		int flow=0;
		while(SPFA(s,t))
		{
			For(i,n) iter[i]=pre[i];
			int f;
			while (f=dfs(s,INF))
				flow+=f; 
		}
		return flow;
	} 
    void mem(int n,int s,int t)  
    {  
        (*this).n=n;
		(*this).t=t;  
		(*this).s=s;  
		
        size=1;  
        MEM(pre) 
    }  
}S;  

int n,f,d;
int main()
{
//	freopen("poj3281.in","r",stdin);
//	freopen(".out","w",stdout);
 	cin>>n>>f>>d;
 	int s=1,t=2+2*n+f+d;
	S.mem(t,1,t);
 	
	For(i,f)
		S.addedge2(s,1+i,1); 
 	
	For(i,d)
		S.addedge2(1+f+2*n+i,t,1); 
 		
	For(i,n)
 	{
 		S.addedge2(1+f+i,1+f+n+i,1);
		int fi,di,p;
 		scanf("%d%d",&fi,&di);
 		For(j,fi)
 		{
 			scanf("%d",&p);
 			S.addedge2(1+p,1+f+i,1);
 		}
 		For(j,di)
 		{
 			scanf("%d",&p);
 			S.addedge2(1+f+n+i,1+f+2*n+p,1);
 		}
 		
 		
 	}
	
	cout<<S.max_flow(s,t)<<endl;
	
	return 0;
}




POJ 3281(Dining-网络流拆点)[Template:网络流dinic]