hdu4292——建模题(EK会超时)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4292

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

题目翻译:

陶妈校赛轻松夺冠,拿到了500的奖金,视金钱为粪土的他决定带着集训队成员出去搓一顿。虽然他很有钱,但是秉承集训队节约的精神,他决定只给每个人一种食物和一种饮料,而且每种饮料和食物都只能提供有限次。现在给出每个人喜欢的食物种类和饮料种类,陶妈可以最多让多少人得到满足呢?聪明的陶妈瞬间得到了问题的答案,但是他想考考你,所以你知道问题的答案吗?

Input

多组测试数据
第一行给出N F D表示人数,食物种类数,饮料种类数
第二行给出F个数字表示第i种食物有几个
第三行给出D个数字表示第i种饮料有几个
接下来N行分别给出第i个人对第j种食物的喜爱情况,Y是喜欢N是不喜欢
再来N行对饮料的

Output

输出陶妈最多能让多少人满意

 

经典卡时间网络流最大流题。

这个题和以前的一道题很类似:POJ3281Dining

基本算是一模一样了。只不过输入变了,其他的建模方式一样。不过我们这里不能用EK算法了,不过除了最浪费时间的EK算法,我们还有Dinic算法,SAP算法以及ISAP算法,应该都能过。这里给出SAP的代码实现。

#include <iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define EMAX 200000
#define VMAX 1005
const int INF = 0x3f3f3f3f;
int head[VMAX],dis[VMAX],cur[VMAX],gap[VMAX],pre[VMAX];
int cnt;
struct edge
{
    int from,to;
    int weight;
    int next;
}e[EMAX];
void addedge(int u,int v,int w) 
{
    e[cnt].to = v;
    e[cnt].weight = w;
    e[cnt].next = head[u];    
    head[u] = cnt++;
    e[cnt].weight = 0;
    e[cnt].to = u;
    e[cnt].next = head[v];    
    head[v] = cnt++;
}
int sap(int s,int t, int n)
{
    memset(dis,0,sizeof(dis));
    memset(gap,0,sizeof(gap));
    for(int i=0; i<=n; i++)
        cur[i] = head[i];
    int u = pre[s];
    pre[s] = s;
    int ret = 0;
    int temp = -1;
    gap[0] = n;
    bool flag;
    while(dis[s] < n)
    {
        flag = false;
        for(int &i = cur[u]; i != -1; i = e[i].next)
        {
            int v = e[i].to;
            if(e[i].weight && dis[u] == dis[v] + 1)
            {
                if (temp == -1 || temp>e[i].weight)
                    temp = e[i].weight;
                pre[v] = u;
                u = v;
                if(v == t)
                {
                    ret += temp;
                    for(u = pre[u];v != s;v = u,u = pre[u])
                    {
                        e[cur[u]].weight -= temp;
                        e[cur[u]^1].weight += temp;
                    }
                    temp = -1;
                }
                flag = true;
                break;
            }
        }
        if (flag)
            continue;

        int mindis = n;
        for(int i = head[u]; i != -1 ; i = e[i].next)
        {
            int v = e[i].to;
            if(e[i].weight && mindis > dis[v])
            {
                cur[u] = i;
                mindis = dis[v];
            }
        }
        gap[dis[u]]--;
        if( gap[dis[u]] == 0)
            break;
        dis[u] = mindis+1;
        gap[dis[u]]++;
        u = pre[u];
    }
    return ret;
}
int main(int argc, char** argv) {
	int n,f,d;
	while(~scanf("%d%d%d",&n,&f,&d)){
		memset(head,-1,sizeof(head));
		int w;
		char str[205];
		cnt=0;
		int S=0;
		int T=n*2+f+d+1;
//		memset(G,0,sizeof(G));
		for(int i = 1;i<=f;++i){
			scanf("%d",&w);
			addedge(S,i,w);
		}
		for(int i = 1;i<=d;++i){
			scanf("%d",&w);
			addedge(i+f+n*2,T,w);
		}
		for(int i = 1;i<=n;++i)
			addedge(f+i,f+n+i,1);
		for(int i = 1;i<=n;++i){
			scanf("%s",str);
			int len=strlen(str);
			for(int j = 0;j<len;++j)
				if(str[j]=='Y')
					addedge(j+1,f+i,1);
//					G[j+1][f+i]=1;
		}
		for(int i = 1;i<=n;++i){
			scanf("%s",str);
			int len=strlen(str);
			for(int j = 0 ;j<len;++j)
				if(str[j]=='Y')
					addedge(i+f+n,f+n*2+1+j,1);
//					G[i+f+n][f+2*n+1+j]=1;
		}
		printf("%d\n",sap(S,T,T+1));
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值