HDU 4292 — Food

来源:互联网 发布:网络协议的作用和功能 编辑:程序博客网 时间:2024/06/05 03:19

原题: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 1e9const 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
原创粉丝点击