hdu4292(网络流)

Food

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6741    Accepted Submission(s): 2279


Problem Description
  You, a part-time dining service worker in your college’s dining hall, are now confused with a new problem: serve as many people as possible.
  The issue comes up as people in your college are more and more difficult to serve with meal: They eat only some certain kinds of food and drink, and with requirement unsatisfied, go away directly.
  You have prepared F (1 <= F <= 200) kinds of food and D (1 <= D <= 200) kinds of drink. Each kind of food or drink has certain amount, that is, how many people could this food or drink serve. Besides, You know there’re N (1 <= N <= 200) people and you too can tell people’s personal preference for food and drink.
  Back to your goal: to serve as many people as possible. So you must decide a plan where some people are served while requirements of the rest of them are unmet. You should notice that, when one’s requirement is unmet, he/she would just go away, refusing any service.
 

Input
  There are several test cases.
  For each test case, the first line contains three numbers: N,F,D, denoting the number of people, food, and drink.
  The second line contains F integers, the ith number of which denotes amount of representative food.
  The third line contains D integers, the ith number of which denotes amount of representative drink.
  Following is N line, each consisting of a string of length F. e jth character in the ith one of these lines denotes whether people i would accept food j. “Y” for yes and “N” for no.
  Following is N line, each consisting of a string of length D. e jth character in the ith one of these lines denotes whether people i would accept drink j. “Y” for yes and “N” for no.
  Please process until EOF (End Of File).
 

Output
  For each test case, please print a single line with one integer, the maximum number of people to be satisfied.
 

Sample Input
  
  
4 3 3 1 1 1 1 1 1 YYN NYY YNY YNY YNY YYN YYN NNY
 

Sample Output
  
  
3


思路:把每个人的节点分成两部分,中间连弧的权值为1,人和喜欢吃的food的权值为1,和喜欢吃的drink的权值为1,超级原点连food,权值为那种food数量,超级汇点连drink,权值为那种drink的数量,跑一遍dinic。好的模板很重要。。。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=1010;
const int maxm=200010;
const int INF=0x3f3f3f3f;
int cnt;  
int s,t;
int n,m;
int Next[maxm];  
int depth[maxn];  
int head[maxn];  
int cur[maxn];  
struct edge{  
    int u,v,w;  
}e[maxm];  
void add_edge(int u,int v,int w) 
{  
    cnt++;  
    Next[cnt]=head[u];  
    head[u]=cnt;  
    e[cnt].u=u;  
    e[cnt].v=v;  
    e[cnt].w=w;  
    cnt++;  
    Next[cnt]=head[v];  
    head[v]=cnt;  
    e[cnt].u=v;  
    e[cnt].v=u;  
    e[cnt].w=0;  
}  
int bfs()  
{  
    memset(depth,-1,sizeof(depth));  
    depth[s]=1;  
    queue<int>q;  
    q.push(s);  
    while(!q.empty())  
    {  
        int u=q.front();  
        q.pop();  
        for(int i=head[u];i!=-1;i=Next[i])  
        {  
            int v=e[i].v;  
            if(depth[v]==-1&&e[i].w>0)  
            {  
                depth[v]=depth[u]+1;  
                q.push(v);  
            }  
        }  
    }  
    if(depth[t]==-1)return 0;  
    return 1;  
}  
int dfs(int u,int w)  
{  
    if(u==t)  
        return w;  
    int f=0;  
    for(int i=head[u];i!=-1;i=Next[i])  
    {  
        int v=e[i].v;  
        if(depth[v]==depth[u]+1&&e[i].w>0)  
        {  
            int d=dfs(v,min(w-f,e[i].w));  
            if(d>0)  
            {  
                e[i].w-=d;  
                e[i^1].w+=d;  
                f+=d;  
                if(f==w)break;  
            }  
        }  
    }  
    if(!f)depth[u]=-2;  
    return f;  
}
int max_flow(int s,int t)
{
	int flow=0;
	while(bfs())
	{
		int f;
		while((f=dfs(s,INF))>0)
		{
			flow+=f;
		}
	}
	return flow;
}
void init()
{
	memset(head,-1,sizeof(head));
	cnt=-1;
}
int main()
{
	int n,f,d;
	while(~scanf("%d%d%d",&n,&f,&d))
	{
		s=f+2*n+d+2;
		t=f+2*n+d+1;
		init();
		int w;
		for(int i=1;i<=f;i++)
		{
			scanf("%d",&w);
			add_edge(s,i,w);
		}
		for(int i=f+2*n+1;i<=f+2*n+d;i++)
		{
			scanf("%d",&w);
			add_edge(i,t,w);
		}
		char ss[300];
		for(int i=1;i<=n;i++)
		{
			scanf("%s",ss+1);
			for(int j=1;j<=f;j++)
			{
				if(ss[j]=='Y')
				{
					add_edge(j,f+i,1);
				}
			}
		}
		for(int i=1;i<=n;i++)
		{
			scanf("%s",ss+1);
			for(int j=1;j<=d;j++)
			{
				if(ss[j]=='Y')
				{
					add_edge(f+n+i,f+2*n+j,1);
				}
			}
		}
		for(int i=1;i<=n;i++)
		{
			add_edge(f+i,f+n+i,1);
		}
		printf("%d\n",max_flow(s,t));
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值