HDU 4292 — Food

原题:http://acm.hdu.edu.cn/showproblem.php?pid=4292

题意:有N个人,F种食物,D种饮料;

    接下来两行分别给出每种食物以及每种饮料的数量;

    下面两个N行 —— 第一个N行表示N个人对这F种食物的喜好,Y表接受,N表拒绝;

     第二个N行表示N个人对这D种饮料的喜好,Y表接受,N表拒绝;


   问最多可以满足几个人的需求;


思路:将N个人拆点为N1和N2,先让源点和食物相连,再让食物和N1个人相连,N1和N2相连,N2再和饮料相连;

    建图 ——

源点s(编号0),F种食物(编号2*N+1到2*N+F), N1个人(编号1到N), 

N2个人(编号1+N到N+N), D种饮料(编号2*N+F+1到2*N+F+D), 汇点t(编号2*N+F+D+1);


    源点和食物之间的边为(s, 2*N+i, 食物数量);食物和N1之间,若为Y则建边为(2*N+j, i, 1);

    N1和N2之间的边为(i, i+N, 1);N2和饮料之间,若为Y则建边为(i+N, 2*N+F+j, 1);

    饮料和汇点之间的边为(2*N+F+i, t,, 饮料数量);



#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#include<iostream>
#include<algorithm>

using namespace std;
#define inf 1e9
const int maxn = 1100;
struct Edge
{
    int from, to, cap, flow;
    Edge(){}
    Edge(int f,int t,int c,int fl):from(f),to(t),cap(c),flow(fl){}
};

int head[maxn], edgenum;
struct Dinic
{
    int n, m, s, t;
    vector<Edge>edges;
    vector<int>G[maxn];
    bool vis[maxn];
    int cur[maxn];
    int d[maxn];

    void init(int n, int s, int t)
    {
        this->n = n, this->s = s, this->t = t;
        edges.clear();
        for(int i = 0;i<n;i++) 
		G[i].clear();
    }

    void add(int from, int to, int cap)
    {
        edges.push_back( Edge(from, to, cap, 0) );
        edges.push_back( Edge(to, from, 0, 0) );
        m = edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool BFS()
    {
        queue<int>q;
        memset(vis, false, sizeof(vis));
        vis[s] = true;
        d[s] = 0;
        q.push(s);
        while(!q.empty())
        {
            int x = q.front(); q.pop();
            for(int i = 0;i<G[x].size();i++)
            {
                Edge& e = edges[G[x][i]];
                if(!vis[e.to] && e.cap>e.flow)
                {
                    vis[e.to] = true;
                    d[e.to] = d[x]+1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int DFS(int x, int a)
    {
        if(x==t || a==0) return a;
        int flow = 0, f;
        for(int &i = cur[x];i<G[x].size();i++)
        {
            Edge &e = edges[G[x][i]];
            if(d[e.to] == d[x]+1 && (f = DFS(e.to,min(a,e.cap-e.flow)))>0)
            {
                e.flow+=f;
                edges[G[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(a == 0) break;
            }
        }
        return flow;
    }

    int maxflow()
    {
        int ans = 0;
        while(BFS())
        {
            memset(cur, 0, sizeof(cur));
            ans+=DFS(s, inf);
        }
        return ans;
    }
}DC;

int main()
{
	int N, F, D;
	while(scanf("%d%d%d", &N, &F, &D)!=EOF)
	{
		memset(head, -1, sizeof(head));
		edgenum = 0;
		int s = 0, t = 2*N+F+D+1;
		DC.init(2*N+F+D+2, s, t);
		for(int i = 1;i<=F;i++)
		{
			int x;
			scanf("%d", &x);
			DC.add(s, 2*N+i, x);
		}
		for(int i = 1;i<=D;i++)
		{
			int x;
			scanf("%d", &x);
			DC.add(2*N+F+i, t, x);
		}
		char str[maxn];
		for(int i = 1;i<=N;i++)
		{
			DC.add(i, i+N, 1);
			scanf("%s", str+1);
			for(int j = 1;j<=F;j++)
			{
				if(str[j] == 'Y')
				DC.add(2*N+j, i, 1);
			}
		}
		for(int i = 1;i<=N;i++)
		{
			scanf("%s", str+1);
			for(int j = 1;j<=D;j++)
			{
				if(str[j] == 'Y')
				DC.add(i+N, 2*N+F+j, 1);
			}
		}
		printf("%d\n", DC.maxflow());
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值