【最大流Dinic模板题】 hdu4292 Food

Food

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


 

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

2012 ACM/ICPC Asia Regional Chengdu Online

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  4288 4296 4295 4294 4293 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAX_V=2005;
const int st=0;
const int en=2000;
const int INF=0x3f3f3f3f;

struct edge
{
    int to,cap,rev; //用于表示边的结构体(终点,容量,反向边)
};

vector <edge> G[MAX_V]; //图的邻接表表示
int level[MAX_V];   //顶点到源点的距离标号
int iter[MAX_V];    //当前弧,在其之前的边已经没有用了
char t[222];

void add_edge(int from,int to,int cap)
{
    G[from].push_back((edge){to,cap,G[to].size()});
    G[to].push_back((edge){from,0,G[from].size()-1});
}

void bfs(int s)  //通过bfs计算从源点出发的距离标号
{
    memset(level,-1,sizeof(level));
    queue <int> q;
    level[s]=0;
    q.push(s);
    while(!q.empty())
    {
        int v=q.front();q.pop();
        for(int i=0;i<G[v].size();i++)
        {
            edge &e=G[v][i];
            if(e.cap>0&&level[e.to]<0)
            {
                level[e.to]=level[v]+1;
                q.push(e.to);
            }
        }
    }
}

int dfs(int v,int t,int f)  //通过dfs寻找增广路
{
    if(v==t) return f;
    for(int &i=iter[v];i<G[v].size();i++)
    {
        edge &e=G[v][i];
        if(e.cap>0&&level[v]<level[e.to])
        {
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0)
            {
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s,int t)
{
    int flow=0;
    for(;;)
    {
        bfs(s); //计算层次图
        if(level[t]<0) return flow; //找不到s-t路径
        memset(iter,0,sizeof(iter)); //初始化当前弧
        int f;
        while((f=dfs(s,t,INF))>0)  //更新最大流
            flow+=f;
    }
    return flow;
}

int main()
{
    int n,f,d;
    while(scanf("%d%d%d",&n,&f,&d)!=EOF)
    {
        for(int i=0;i<MAX_V;i++) G[i].clear();
        for(int i=1;i<=f;i++)
        {
            int x;scanf("%d",&x);
            add_edge(i,i+200,x);  //每种food有几个
            add_edge(st,i,INF);   //起点到food
        }
        for(int i=1;i<=d;i++)
        {
            int x;scanf("%d",&x);
            add_edge(i+800,i+1000,x);  //每种饮料有几个
            add_edge(i+1000,en,INF);   //饮料到终点
        }
        for(int i=1;i<=n;i++) add_edge(i+400,i+600,1); //人自己连,注意是1
        for(int i=1;i<=n;i++)
        {
            scanf("%s",t+1);
            for(int j=1;j<=f;j++)
                if(t[j]=='Y') add_edge(j+200,i+400,INF);  //人和food
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%s",t+1);
            for(int j=1;j<=d;j++)
                if(t[j]=='Y') add_edge(i+600,j+800,INF);  //人到饮料
        }
        printf("%d\n",max_flow(st,en));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41037114/article/details/81486048
今日推荐