Hdu4292 food --- maximum stream

Source: Internet
Author: User

N people, F kinds of food, d kinds of drinks, each have a certain number, each person has their own preferences for each type of food and beverage, must satisfy y at the same time.

How many people can be satisfied.


Add Source Vertex S and sink vertex T.

S to the number of foods with the edge weight.

According to everyone's preferences, food is sent to people, people are sent to drinks, and edge weight is 1.

Each person can only meet the requirements once, so the person is split and connected to his own side, the edge right is 1.

Finally, the edge weight of each type of beverage to T is the number of such drinks.



#include<cstdio>#include<vector>#include<cstring>#include<queue>#define maxn 1010#define inf 0x3f3f3f3fusing namespace std;struct node{    int from,to,cap,flow;};struct dinic{    int n,m,s,t;    vector<node> e;    vector<int> g[maxn];    bool vis[maxn];    int d[maxn];    int cur[maxn];    void init(int n)    {        e.clear();        for(int i=0;i<=n+2;i++)            g[i].clear();    }    void addedge(int a,int b,int c,int d)    {        e.push_back((node){a,b,c,0});        e.push_back((node){b,a,d,0});        m=e.size();        g[a].push_back(m-2);        g[b].push_back(m-1);    }    bool bfs()    {        memset(vis,0,sizeof vis);        queue<int> q;        q.push(s);        d[s]=0;        vis[s]=1;        while(!q.empty())        {            int x=q.front();q.pop();            for(int i=0;i<g[x].size();i++)            {                node& ee=e[g[x][i]];                if(!vis[ee.to]&&ee.cap>ee.flow)                {                    vis[ee.to]=1;                    d[ee.to]=d[x]+1;                    q.push(ee.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++)        {            node& ee=e[g[x][i]];            if(d[x]+1==d[ee.to]&&(f=dfs(ee.to,min(a,ee.cap-ee.flow)))>0)            {                ee.flow+=f;                e[g[x][i]^1].flow-=f;                flow+=f;                a-=f;                if(a==0) break;            }        }        return flow;    }    int maxflow(int s,int t)    {        this->s=s;        this->t=t;        int flow=0;        while(bfs())        {            memset(cur,0,sizeof cur);            flow+=dfs(s,inf);        }        return flow;    }};dinic solve;int main(){    int i,j,a,n,f,d,s,t;    char str[210];    while(~scanf("%d%d%d",&n,&f,&d))    {        s=0,t=f+n+n+d+1;        solve.init(t);        for(i=1;i<=f;i++)        {            scanf("%d",&a);            solve.addedge(s,i,a,0);        }        for(i=f+1;i<=f+n;i++) solve.addedge(i,i+n,1,0);        for(i=1;i<=d;i++)        {            scanf("%d",&a);            solve.addedge(i+n+n+f,t,a,0);        }        for(i=1;i<=n;i++)        {            scanf("%s",str);            for(j=1;j<=f;j++) if(str[j-1]=='Y')                solve.addedge(j,f+i,1,0);        }        for(i=1;i<=n;i++)        {            scanf("%s",str);            for(j=1;j<=d;j++) if(str[j-1]=='Y')                solve.addedge(f+n+i,f+n+n+j,1,0);        }        printf("%d\n",solve.maxflow(s,t));    }    return 0;}


Hdu4292 food --- maximum stream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.