hdu4292

来源:互联网 发布:dd是什么意思网络用语 编辑:程序博客网 时间:2024/06/05 18:05



Food

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


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 31 1 11 1 1YYNNYYYNYYNYYNYYYNYYNNNY
 

Sample Output
3
输入三个数,人数,食品数,和饮料数,每个人只能选择一种视频和饮料,问最多能让多少人得到最大的选择,前几天看了匈牙利算法但是没看看完,看这个匹配还以为是,结果原来是最大流,关键在图建的好,顺序无关,但是要让两种食品分别在他两边,那个数量就是流量,其他的全设置成1

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define MAXN 1000
#define MAXM 200000+10
#define INF 0x3f3f3f3f
using namespace std;
struct Edge
{
    int from, to, cap, flow, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int dist[MAXN], cur[MAXN];
bool vis[MAXN];
int N, F, D;
int source, sink;//超级源点 超级汇点
void init()
{
    edgenum = 0;
    memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w)
{
    Edge E1 = {u, v, w, 0, head[u]};
    edge[edgenum] = E1;
    head[u] = edgenum++;
    Edge E2 = {v, u, 0, 0, head[v]};
    edge[edgenum] = E2;
    head[v] = edgenum++;
}
void getMap()
{
    int a;
    source = 0, sink = 2*N + F + D + 1;
    //人拆点后 编号从1到2*N
    for(int i = 1; i <= N; i++)//对人拆点
        addEdge(i, i+N, 1);//只能选一种食物和一种饮料
    //食物编号从2*N+1到2*N+F
    for(int i = 1; i <= F; i++)
    {
        scanf("%d", &a);
        addEdge(source, 2*N+i, a);//源点 到 食物建边
    }
    //饮料编号2*N+F+1到2*N+F+D
    for(int i = 1; i <= D; i++)
    {
        scanf("%d", &a);
        addEdge(2*N+F+i, sink, a);//饮料 到 汇点建边
    }
    char str[300];
    for(int i = 1; i <= N; i++)
    {
        scanf("%s", str);
        for(int j = 0; j < F; j++)
        {
            if(str[j] == 'Y')//第i个人 接受 第j+1种食物
                addEdge(2*N+j+1, i, 1);
        }
    }
    for(int i = 1; i <= N; i++)
    {
        scanf("%s", str);
        for(int j = 0; j < D; j++)
        {
            if(str[j] == 'Y')//第i个人 接受 第j+1种饮料
                addEdge(i+N, 2*N+F+j+1, 1);
        }
    }
}
/*bool BFS(int s, int t)
{
    queue<int> Q;
    memset(dist, -1, sizeof(dist));
    memset(vis, false, sizeof(vis));
    dist[s] = 0;
    vis[s] = true;
    Q.push(s);
    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            Edge E = edge[i];
            if(!vis[E.to] && E.cap > E.flow)
            {
                dist[E.to] = dist[u] + 1;
                if(E.to == t) return true;
                vis[E.to] = true;
                Q.push(E.to);
            }
        }
    }
    return false;
}*/
bool BFS(int s,int t)
{
     queue <int>Q;//存东西用
     memset(dist,-1,sizeof(dist));
     memset(vis,false,sizeof(vis));
     dist[s]=0;//分层,先给第一层个0:
     vis[s]=true;
     Q.push(s);
     while(!Q.empty())
   {   int u=Q.front();//从门口拿走一个数;
        Q.pop();
       for(int i=head[u];i!=-1;i=edge[i].next)
    {  Edge a=edge[i];
       if(!vis[a.to]&&a.cap>a.flow)
       {   dist[a.to]=dist[u]+1;
           if(a.to==t)return true;
            vis[a.to]=true;
             Q.push(a.to);
       }








    }
   }
    return false;




}
int DFS(int x, int a, int t)
{
    if(x == t || a == 0) return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next)
    {
        Edge &E = edge[i];
        if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap - E.flow), t)) > 0)
        {
            edge[i].flow += f;//为啥加的 都是反的


            edge[i^1].flow -= f;
            flow += f;
            a -= f;
            if(a == 0) break;
        }
    }
    return flow;
}
//这样写有一个好处,就是如果想得到最大流的流向,
//那么直接就把flow提出来就可以了
int Maxflow(int s, int t)
{
    int flow = 0;
    while(BFS(s, t))
    {
        memcpy(cur, head, sizeof(head));
        flow += DFS(s, INF, t);
    }
    return flow;
}
int main()
{
    while(scanf("%d%d%d", &N, &F, &D) != EOF)
    {
        init();
        getMap();
        printf("%d\n", Maxflow(source, sink));
    }
    return 0;
}

1 0
原创粉丝点击