HDU4292 网络流 2012 ACM/ICPC Asia Regional Chengdu Online1005

                                   Food

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

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

 

题解:比赛的时候没看 现在看了一下很简单的网络流问题 开一个F+D+2*N+2的一个网络 源点与各个食物点F相连 权值为各个食物的权值 再将食物点按照字符条件 将食物点与前N个顾客点对应条件一一相连 权值为1 为了保证顾客不会获得两种或者两种以上的食物或饮料 将前N个顾客点与后N个顾客点相连 权值为1 再将后N个顾客点与饮料点相连 权值为1 再将饮料点与汇点相连 就可以找最大值了 这题起初我还考虑有的顾客只要食物
或者有的顾客只要饮料 但是这种测试数据里好像没有 所以我程序也没加

 

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

using namespace std;
const int oo=1e9;
/**oo表示无穷大*/
const int mm=111111111;
/**mm表示边的最大数量,记住要是原图的两倍,在加边的时候都是双向的*/
const int mn=9999999;
/**mn表示点的最大数量*/
int node,src,dest,edge;
/**node表示节点数,src表示源点,dest表示汇点,edge统计边数*/
int ver[mm],flow[mm],next[mm];
/**ver 边指向的节点,flow 边的容量 ,next 链表的下一条边*/
int head[mn],work[mn],dis[mn],q[mn];
/**head 节点的链表头,work 用于算法中的临时链表头,dis 计算距离*/

/**初始化链表及图的信息*/
void prepare(int _node,int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0; i<node; ++i)head[i]=-1;
    edge=0;
}
/**增加一条u到v容量为c的边*/
void addedge(int u,int v,int c)
{
    ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
/**广搜计算出每个点与源点的最短距离,如果不能到达汇点说明算法结束*/
bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0; i<node; ++i)dis[i]=-1;
    dis[q[r++]=src]=0;
    for(l=0; l<r; ++l)
        for(i=head[u=q[l]]; i>=0; i=next[i])
            if(flow[i]&&dis[v=ver[i]]<0)
            {
                /**这条边必须有剩余容量*/
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)return 1;
            }
    return 0;
}
/**寻找可行流的增广路算法,按节点的距离来找,加快速度*/
int Dinic_dfs(int u,int exp)
{
    if(u==dest)return exp;
    /**work是临时链表头,这里用i引用它,这样寻找过的边不再寻找*/
    for(int &i=work[u],v,tmp; i>=0; i=next[i])
        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            /**正反向边容量改变*/
            return tmp;
        }
    return 0;
}
/**求最大流,直到没有可行流*/
int Dinic_flow()
{
    int i,ret=0,delta;
    while(Dinic_bfs())
    {
        for(i=0; i<node; ++i)work[i]=head[i];
        while(delta=Dinic_dfs(src,oo))ret+=delta;
    }
    return ret;
}

int main()
{
    int n,f,d,ef,ed,ans;
    string sf,sd;
    while(cin>>n>>f>>d)
    {
        prepare(f+d+2*n+2,0,f+d+2*n+1);
        for(int i=1; i<=f; i++)
        {
            cin>>ef;
            addedge(src,i,ef);
        }
        for(int i=1; i<=d; i++)
        {
            cin>>ed;
            addedge(f+n+n+i,dest,ed);
        }
        for(int i=1; i<=n; i++)
        {
            addedge(f+i,f+n+i,1);
            cin>>sf;
            for(int j=0; j<f; j++)
                if(sf[j]=='Y')
                    addedge(j+1,f+i,1);
        }
        for(int i=1; i<=n; i++)
        {
            cin>>sd;
            for(int j=0; j<d; j++)
                if(sd[j]=='Y')
                    addedge(f+n+i,f+n+n+j+1,1);
        }
        ans=Dinic_flow();
        cout<<ans<<endl;
    }

    return 0;
}
时间: 2025-01-01 08:11:21

HDU4292 网络流 2012 ACM/ICPC Asia Regional Chengdu Online1005的相关文章

HDU4291 循环节+矩阵连乘2012 ACM/ICPC Asia Regional Chengdu Online1004

                            A Short problem                                                Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)                                                            Total Submission(s)

hdu 5461 Largest Point (2015 ACM/ICPC Asia Regional Shenyang Online)

点击打开链接 Largest Point Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 474    Accepted Submission(s): 213 Problem Description Given the sequence A with n integers t1,t2,⋯,tn. Given the integral c

hdu 5455 Fang Fang(2015 ACM/ICPC Asia Regional Shenyang Online)

点击打开链接 Fang Fang Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 531    Accepted Submission(s): 232 Problem Description Fang Fang says she wants to be remembered. I promise her. We define the s

Java in ACM/ICPC

目录 Java在ACM/ICPC中的特点 在ACM/ICPC中使用Java需要注意的问题 Java与高精度计算 1.Java在ACM/ICPC中的特点 Java的语法和C++几乎相同 Java在执行计算密集任务的时候并不比C/C++慢多少,只是IO操作较慢而已 Java 简单而功能强大,有些东西用Java实现起来更为方便 比如:输入输出.字符串解析.高精度 Java不易犯细微的错误 C/C++中的指针 "if (n = m) ... " Java与Eclipse 2.在ACM/ICPC

《程序设计解题策略》——第1章 利用树型数据关系的解题策略 1.1 利用划分树求解整数区间内第k大的值

第1章 利用树型数据关系的解题策略 树是一个具有层次结构的集合,一种限制前件数且没有回路的连通图.在现实生活和程序设计的竞赛试题中,许多问题的数据关系呈树型结构,因此有关树的概念.原理.操作方法和一些由树的数据结构支持的算法,一直受到编程者的重视,被广泛应用于解题过程.在本章里,我们将介绍利用树型数据关系解题的七种策略: 1) 利用划分树求解整数区间内第k大值. 2) 利用最小生成树及其扩展形式(最优比率生成树.最小k度生成树.次小生成树)计算有权连通无向图中边权和满足限制条件的最优生成树. 3

HDU4730-We Love MOE Girls

We Love MOE Girls Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1047    Accepted Submission(s): 636 Problem Description Chikami Nanako is a girl living in many different parallel worlds. In th

hdu 5491 The Next(ICPC合肥赛)

The Next Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 808    Accepted Submission(s): 316 Problem Description Let L denote the number of 1s in integer D's binary representation. Given two int

HDU 4009 Transfer water:最小树形图

链接: http://acm.hdu.edu.cn/showproblem.php?pid=4009 题目: Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 2508    Accepted Submission(s): 934 Problem Description XiaoA lives in a vi

HDU4706-Children&amp;#39;s Day

Children's Day Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 781    Accepted Submission(s): 501 Problem Description Today is Children's Day. Some children ask you to output a big letter 'N'.