HDU 4292 Food (SAP | Dinic )

Food

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1491    Accepted Submission(s): 534


Problem Description
  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
 

 

Source
 

 

Recommend
liuyiding

 

 

题意:有F种食物 D种饮料 它们都有一定的数量 有N个人 每个人都有自己喜欢吃的食物和饮料 (每个人至少要一种食物和饮料) 只有能满足他的要求时他才会接服务 求最大能满足多少人?

思路:网络流 建一超级源点 汇点 源点与食物相连 边权为其数量,汇点与饮料相连 边权也为其数量 把人分成两个点 之间的边权为1 每个人与之需要的食物和饮料相连 边权为1 (或者INF)

 当然,这题和POJ 3281 Dining很类似:http://poj.org/problem?id=3281

 

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int VM=1010;
const int EM=200010;
const int INF=0x3f3f3f3f;

struct Edge{
    int to,nxt;
    int cap;
}edge[EM<<1];

int N,F,D,cnt,head[VM],map[110][110];
int dep[VM],gap[VM],cur[VM],aug[VM],pre[VM];

void addedge(int cu,int cv,int cw){
    edge[cnt].to=cv;  edge[cnt].cap=cw;  edge[cnt].nxt=head[cu];
    head[cu]=cnt++;
    edge[cnt].to=cu;  edge[cnt].cap=0;   edge[cnt].nxt=head[cv];
    head[cv]=cnt++;
}

int src,des;

int SAP(int n){
    int max_flow=0,u=src,v;
    int id,mindep;
    aug[src]=INF;
    pre[src]=-1;
    memset(dep,0,sizeof(dep));
    memset(gap,0,sizeof(gap));
    gap[0]=n;
    for(int i=0;i<=n;i++)
        cur[i]=head[i]; // 初始化当前弧为第一条弧
    while(dep[src]<n){
        int flag=0;
        if(u==des){
            max_flow+=aug[des];
            for(v=pre[des];v!=-1;v=pre[v]){     // 路径回溯更新残留网络
                id=cur[v];
                edge[id].cap-=aug[des];
                edge[id^1].cap+=aug[des];
                aug[v]-=aug[des];   // 修改可增广量,以后会用到
                if(edge[id].cap==0) // 不回退到源点,仅回退到容量为0的弧的弧尾
                    u=v;
            }
        }
        for(int i=cur[u];i!=-1;i=edge[i].nxt){
            v=edge[i].to;    // 从当前弧开始查找允许弧
            if(edge[i].cap>0 && dep[u]==dep[v]+1){  // 找到允许弧
                flag=1;
                pre[v]=u;
                cur[u]=i;
                aug[v]=min(aug[u],edge[i].cap);
                u=v;
                break;
            }
        }
        if(!flag){
            if(--gap[dep[u]]==0)    /* gap优化,层次树出现断层则结束算法 */
                break;
            mindep=n;
            cur[u]=head[u];
            for(int i=head[u];i!=-1;i=edge[i].nxt){
                v=edge[i].to;
                if(edge[i].cap>0 && dep[v]<mindep){
                    mindep=dep[v];
                    cur[u]=i;   // 修改标号的同时修改当前弧
                }
            }
            dep[u]=mindep+1;
            gap[dep[u]]++;
            if(u!=src)  // 回溯继续寻找允许弧
                u=pre[u];
        }
    }
    return max_flow;
}

int main(){

    //freopen("input.txt","r",stdin);

    while(~scanf("%d%d%d",&N,&F,&D)){
        cnt=0;
        memset(head,-1,sizeof(head));
        int f,d;
        src=0,  des=F+2*N+D+1;
        for(int i=1;i<=N;i++){
            scanf("%d%d",&f,&d);
            int x;
            for(int j=1;j<=f;j++){  
                scanf("%d",&x);
                addedge(x,F+i,1);   //食物和牛1(将牛分成两点)相连
            }
            for(int j=1;j<=d;j++){
                scanf("%d",&x);
                addedge(F+N+i,F+2*N+x,1);   //牛2和饮料相连
            }
            addedge(F+i,F+N+i,1);   //牛1和牛2相连,保证没头牛只吃一种食物和饮料
        }
        for(int i=1;i<=F;i++)
            addedge(src,i,1);   //超级源点与食物相连
        for(int i=1;i<=D;i++)
            addedge(F+2*N+i,des,1);     //饮料与超级汇点相连
        printf("%d\n",SAP(des+1));
    }
    return 0;
}
View Code

 

本题代码:

 

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int VM=1010;
const int EM=200010;
const int INF=0x3f3f3f3f;

struct Edge{
    int to,nxt;
    int cap;
}edge[EM<<1];

int N,F,D,cnt,head[VM],map[110][110];
int dep[VM],gap[VM],cur[VM],aug[VM],pre[VM];

void addedge(int cu,int cv,int cw){
    edge[cnt].to=cv;  edge[cnt].cap=cw;  edge[cnt].nxt=head[cu];
    head[cu]=cnt++;
    edge[cnt].to=cu;  edge[cnt].cap=0;   edge[cnt].nxt=head[cv];
    head[cv]=cnt++;
}

int src,des;

int SAP(int n){
    int max_flow=0,u=src,v;
    int id,mindep;
    aug[src]=INF;
    pre[src]=-1;
    memset(dep,0,sizeof(dep));
    memset(gap,0,sizeof(gap));
    gap[0]=n;
    for(int i=0;i<=n;i++)
        cur[i]=head[i]; // 初始化当前弧为第一条弧
    while(dep[src]<n){
        int flag=0;
        if(u==des){
            max_flow+=aug[des];
            for(v=pre[des];v!=-1;v=pre[v]){     // 路径回溯更新残留网络
                id=cur[v];
                edge[id].cap-=aug[des];
                edge[id^1].cap+=aug[des];
                aug[v]-=aug[des];   // 修改可增广量,以后会用到
                if(edge[id].cap==0) // 不回退到源点,仅回退到容量为0的弧的弧尾
                    u=v;
            }
        }
        for(int i=cur[u];i!=-1;i=edge[i].nxt){
            v=edge[i].to;    // 从当前弧开始查找允许弧
            if(edge[i].cap>0 && dep[u]==dep[v]+1){  // 找到允许弧
                flag=1;
                pre[v]=u;
                cur[u]=i;
                aug[v]=min(aug[u],edge[i].cap);
                u=v;
                break;
            }
        }
        if(!flag){
            if(--gap[dep[u]]==0)    /* gap优化,层次树出现断层则结束算法 */
                break;
            mindep=n;
            cur[u]=head[u];
            for(int i=head[u];i!=-1;i=edge[i].nxt){
                v=edge[i].to;
                if(edge[i].cap>0 && dep[v]<mindep){
                    mindep=dep[v];
                    cur[u]=i;   // 修改标号的同时修改当前弧
                }
            }
            dep[u]=mindep+1;
            gap[dep[u]]++;
            if(u!=src)  // 回溯继续寻找允许弧
                u=pre[u];
        }
    }
    return max_flow;
}

int main(){

    //freopen("input.txt","r",stdin);

    char str[220];
    while(~scanf("%d%d%d",&N,&F,&D)){
        cnt=0;
        memset(head,-1,sizeof(head));
        int f,d;
        src=0, des=F+2*N+D+1;
        for(int i=1;i<=F;i++){
            scanf("%d",&f);
            addedge(src,i,f);
        }
        for(int i=F+2*N+1;i<=F+2*N+D;i++){
            scanf("%d",&d);
            addedge(i,des,d);
        }
        for(int i=1;i<=N;i++){
            scanf("%s",str);
            for(int j=0;j<F;j++)
                if(str[j]=='Y')
                    addedge(j+1,F+i,1);     //这里权值为INF亦可
        }
        for(int i=1;i<=N;i++){
            scanf("%s",str);
            for(int j=0;j<D;j++)
                if(str[j]=='Y')
                    addedge(F+N+i,F+2*N+j+1,1);     //这里权值为INF亦可
        }
        for(int i=F+1;i<=F+N;i++)   
            addedge(i,i+N,1);   //将人拆分成两点,边权为1,为了控制最多的人得到一个食物以及一瓶饮料
        printf("%d\n",SAP(des+1));
    }
    return 0;
}

 

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>

using namespace std;

const int VM=1010;
const int EM=200010;
const int INF=0x3f3f3f3f;

struct Edge{
    int to,nxt;
    int cap;
}edge[EM<<1];

int N,F,D,cnt,head[VM],src,des;
int dep[VM];

void addedge(int cu,int cv,int cw){
    edge[cnt].to=cv;    edge[cnt].cap=cw;   edge[cnt].nxt=head[cu];
    head[cu]=cnt++;
    edge[cnt].to=cu;    edge[cnt].cap=0;    edge[cnt].nxt=head[cv];
    head[cv]=cnt++;
}

int BFS(){
    queue<int> q;
    while(!q.empty())
        q.pop();
    memset(dep,-1,sizeof(dep));
    dep[src]=0;
    q.push(src);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=edge[i].nxt){
            int v=edge[i].to;
            if(edge[i].cap>0 && dep[v]==-1){
                dep[v]=dep[u]+1;
                q.push(v);
            }
        }
    }
    return dep[des]!=-1;
}

int DFS(int u,int minx){
    if(u==des)
        return minx;
    int tmp;
    for(int i=head[u];i!=-1;i=edge[i].nxt){
        int v=edge[i].to;
        if(edge[i].cap>0 && dep[v]==dep[u]+1 && (tmp=DFS(v,min(minx,edge[i].cap)))){
            edge[i].cap-=tmp;
            edge[i^1].cap+=tmp;
            return tmp;
        }
    }
    dep[u]=-1;
    return 0;
}

int Dinic(){
    int ans=0,tmp;
    while(BFS()){
        while(1){
            tmp=DFS(src,INF);
            if(tmp==0)
                break;
            ans+=tmp;
        }
    }
    return ans;
}

int main(){

    //freopen("input.txt","r",stdin);

    char str[220];
    while(~scanf("%d%d%d",&N,&F,&D)){
        cnt=0;
        memset(head,-1,sizeof(head));
        int f,d;
        src=0, des=F+2*N+D+1;
        for(int i=1;i<=F;i++){
            scanf("%d",&f);
            addedge(src,i,f);
        }
        for(int i=F+2*N+1;i<=F+2*N+D;i++){
            scanf("%d",&d);
            addedge(i,des,d);
        }
        for(int i=1;i<=N;i++){
            scanf("%s",str);
            for(int j=0;j<F;j++)
                if(str[j]=='Y')
                    addedge(j+1,F+i,1);
        }
        for(int i=1;i<=N;i++){
            scanf("%s",str);
            for(int j=0;j<D;j++)
                if(str[j]=='Y')
                    addedge(F+N+i,F+2*N+j+1,1);
        }
        for(int i=F+1;i<=F+N;i++)
            addedge(i,i+N,1);
        printf("%d\n",Dinic());
    }
    return 0;
}
posted @ 2013-04-14 10:32  Jack Ge  阅读(349)  评论(0编辑  收藏  举报