zoukankan      html  css  js  c++  java
  • poj3281 Dining[最大流]

    Dining
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 17181   Accepted: 7607

    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

     Select Code

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #define EF if(ch==EOF) return x;
    using namespace std;
    const int N=405;
    const int M=1e5+5;
    const int inf=2e9;
    struct edge{int v,next,cap;}e[M<<1];int tot=1,head[N];
    int n,m,ans,f,d,S,T,dis[N],q[N*2];
    inline int read(){
    	int x=0,f=1;char ch=getchar();
    	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;EF;ch=getchar();}
    	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    	return x*f;
    }
    void add(int x,int y,int z){
    	e[++tot].v=y;e[tot].cap=z;e[tot].next=head[x];head[x]=tot;
    	e[++tot].v=x;e[tot].cap=0;e[tot].next=head[y];head[y]=tot;
    }
    bool bfs(){
    	memset(dis,-1,sizeof dis);
    	int h=0,t=1;q[t]=S;dis[S]=0;
    	while(h!=t){
    		int x=q[++h];
    		for(int i=head[x];i;i=e[i].next){
    			if(e[i].cap&&dis[e[i].v]==-1){
    				dis[e[i].v]=dis[x]+1;
    				if(e[i].v==T) return 1;
    				q[++t]=e[i].v;
    			}
    		} 
    	}
    	return 0;
    }
    int dfs(int x,int f){
    	if(x==T) return f;
    	int used=0,t;
    	for(int i=head[x];i;i=e[i].next){
    		if(e[i].cap&&dis[e[i].v]==dis[x]+1){
    			t=dfs(e[i].v,min(e[i].cap,f));
    			e[i].cap-=t;e[i^1].cap+=t;
    			used+=t;f-=t;
    			if(!f) return used;
    		}
    	}
    	if(!used) dis[x]=-1;
    	return used;
    }
    void dinic(){
    	ans=0;
    	while(bfs()) ans+=dfs(S,inf);
    }
    void work(){
    	n=read();f=read();d=read();S=0;T=f+n+n+d+1;
    	for(int i=1,fi,di,x;i<=n;i++){
    		fi=read();
    		di=read();
    		while(fi--){
    			x=read();
    			add(x,f+i,1);
    		}
    		while(di--){
    			x=read();
    			add(f+n+i,f+n+n+x,1);
    		}
    	}
    	for(int i=1;i<=n;i++) add(f+i,f+n+i,1);
    	for(int i=1;i<=f;i++) add(S,i,1);
    	for(int i=1;i<=d;i++) add(f+n+n+i,T,1);
    	dinic();
    	printf("%d
    ",ans);
    }
    int main(){
    	work();
    	return 0;
    }
  • 相关阅读:
    204. 计数质数
    236. 二叉树的最近公共祖先
    优先队列和哈夫曼树
    185. 部门工资前三高的所有员工(求组内前几的值)
    部门工资最高的员工(求组内最大值)
    回调函数的案例
    单链表
    动态数组
    一致性哈希算法的基本原理
    只用2GB内存在20亿个整数中找到出现次数最多的数
  • 原文地址:https://www.cnblogs.com/shenben/p/6664505.html
Copyright © 2011-2022 走看看