[拆点+网络流]2012 acm/icpc成都网络赛 hdoj 4292:Food

大致题意:

    有F种食物和D种饮料,每种食物或饮料只能供有限次,且每个人只享用一种食物和一种饮料。现在有n头牛,每个人都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几个人同时享用到自己喜欢的食物和饮料。

 

大致思路:

    把人给拆点,把食物和水的点放在人的两侧


[拆点+网络流]2012 acm/icpc成都网络赛 hdoj 4292:Food

求出s到t的网络流即可

 

 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int inf=1<<30;
const int nMax=101050;
const int mMax=3000000;

class node{
    public:
    int c,u,v,next;
};node edge[mMax];
int ne, head[nMax];
int cur[nMax], ps[nMax], dep[nMax];

void addedge(int u, int v,int w){   ////dinic邻接表加边
  //  cout<<u<<" add "<<v<<" "<<w<<endl;
    edge[ne].u = u;
    edge[ne].v = v;
    edge[ne].c = w;
    edge[ne].next = head[u];
    head[u] = ne ++;
    edge[ne].u = v;
    edge[ne].v = u;
    edge[ne].c = 0;
    edge[ne].next = head[v];
    head[v] = ne ++;
}

int dinic(int s, int t){                       //  dinic
    int tr, res = 0;
    int i, j, k, f, r, top;
    while(1){
        memset(dep, -1, sizeof(dep));
        for(f = dep[ps[0]=s] = 0, r = 1; f != r;)
            for(i = ps[f ++], j = head[i]; j; j = edge[j].next)
                if(edge[j].c && dep[k=edge[j].v] == -1){
                    dep[k] = dep[i] + 1;
                    ps[r ++] = k;
                    if(k == t){
                        f = r; break;
                    }
                }
        if(dep[t] == -1) break;
        memcpy(cur, head, sizeof(cur));
        i = s, top = 0;
        while(1){
            if(i == t){
                for(tr =inf, k = 0; k < top; k ++)
                    if(edge[ps[k]].c < tr)
                        tr = edge[ps[f=k]].c;
                for(k = 0; k < top; k ++){
                    edge[ps[k]].c -= tr;
                    edge[ps[k]^1].c += tr;
                }
                i = edge[ps[top=f]].u;
                res += tr;
            }
            for(j = cur[i]; cur[i]; j = cur[i] =edge[cur[i]].next){
                if(edge[j].c && dep[i]+1 == dep[edge[j].v]) break;
            }
            if(cur[i]){
                ps[top ++] = cur[i];
                i = edge[cur[i]].v;
            }
            else{
                if(top == 0) break;
                dep[i] = -1;
                i = edge[ps[-- top]].u;
            }
        }
    }
    return res;
}

int food [10000];
int drink[10000];
char str[500][500];
int main()
{
    int n,i,j,a,b,c,nf,nd,s,t;
    while(scanf("%d%d%d",&n,&nf,&nd)!=EOF)
    {
        s=0,t=nf+nd+n+n+10;
        ne=2;
        memset(head,0,sizeof(head));
        for(i=1;i<=nf;i++)
        {
            scanf("%d",&food[i]);
            addedge(0,i,food[i]);
        }
        for(i=1;i<=nd;i++)
        {
            scanf("%d",&drink[i]);
            addedge(i+nf,t,drink[i]);
        }
        for(i=1;i<=n;i++)
        {
            addedge(nd+nf+i,nd+nf+n+i,1);
        }
        for(i=1;i<=n;i++)
        {
            scanf("%s",str[i]+1);
            for(j=1;j<=nf;j++)
            {
                if(str[i][j]=='Y')
                {
                    addedge(j,nf+nd+i,1);
                }
            }
        }

        for(i=1;i<=n;i++)
        {
            scanf("%s",str[i]+1);
            for(j=1;j<=nd;j++)
            {
                if(str[i][j]=='Y')
                {
                    addedge(nf+nd+n+i,nf+j,1);
                }
            }
        }
        cout<<dinic(s,t)<<endl;
    }
    return 0;
}

你可能感兴趣的:(ACM,bbezxcy,图论,网络流,成都网络赛)