zoukankan      html  css  js  c++  java
  • [Poj3281]Dining(最大流)

    Description

    有n头牛,f种食物,d种饮料,每头牛有nf种喜欢的食物,nd种喜欢的饮料,每种食物如果给一头牛吃了,那么另一个牛就不能吃这种食物了,饮料也同理,问最多有多少头牛可以吃到它喜欢的饮料和食物。

    Solution

    巧妙地建一下图跑最大流即可

    因为有食物和饮料2种条件,不难想到把牛放在中间,食物饮料放两边连边建图

    但实际上这样可能会让一头牛被重复计入答案,一头牛可能同时满足两种不同的方案

    所以把牛拆成2个点即可

    Code

    #include <cstdio>
    #include <algorithm>
    #define N 666
    #define Inf 0x7fffffff
    using namespace std;
    
    struct info{int to,nex,f;}e[100010];
    int n,f,d,T,S,tot,nodes,head[N],Ans,cnt[N],dis[N];
    
    inline void Link(int u,int v,int f){
    	e[++tot].to=v;e[tot].nex=head[u];head[u]=tot;e[tot].f=f;
    	e[++tot].to=u;e[tot].nex=head[v];head[v]=tot;e[tot].f=0;
    }
    
    inline int read(){
    	int x=0,f=1;char ch=getchar();
    	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    	return x*f;
    }
    
    inline void Init(){
    	n=read(),f=read(),d=read();
    	S=0,tot=1,nodes=(T=f+d+n*2+1)+1;
    	for(int i=1;i<=f;++i) Link(S,i,1);
    	for(int i=1;i<=d;++i) Link(f+n*2+i,T,1);
    	for(int i=1;i<=n;++i) Link(f+i,f+n+i,1);
    	for(int i=1;i<=n;++i){
    		int nf=read(),nd=read();
    		while(nf--){int t=read();Link(t,i+f,1);}
    		while(nd--){int t=read();Link(i+f+n,f+n*2+t,1);}
    	}
    }
    
    int sap(int u,int d){
    	if(u==T) return d;
    	int sum=0,mins=nodes;
    	for(int i=head[u];i;i=e[i].nex){
    		int v=e[i].to;
    		if(e[i].f>0&&dis[u]==dis[v]+1){
    			int save=sap(v,min(d-sum,e[i].f));
    			sum+=save;
    			e[i].f-=save;
    			e[i^1].f+=save;
    			if(dis[S]>=nodes||sum==d) return sum;
    		}
    		if(e[i].f>0) mins=min(mins,dis[v]);
    	}
    	if(!sum){
    		if(!(--cnt[dis[u]])) dis[S]=nodes;
    		else ++cnt[dis[u]=mins+1];
    	}
    	return sum;
    }
    
    void SAP(){cnt[0]=nodes;while(dis[S]<nodes) Ans+=sap(S,Inf);}
    
    int main(){
    	Init();
    	SAP();
    	printf("%d
    ",Ans);
    	return 0;
    }
    
    
  • 相关阅读:
    优化SQL查询:如何写出高性能SQL语句
    提高SQL执行效率的16种方法
    Spring Ioc DI 原理
    java内存泄漏
    转:js闭包
    LeetCode Best Time to Buy and Sell Stock III
    LeetCode Best Time to Buy and Sell Stock with Cooldown
    LeetCode Length of Longest Fibonacci Subsequence
    LeetCode Divisor Game
    LeetCode Sum of Even Numbers After Queries
  • 原文地址:https://www.cnblogs.com/void-f/p/8463422.html
Copyright © 2011-2022 走看看