HDU4292 Food (network flow, Max Stream, Dinic algorithm)

Source: Internet
Author: User
Description

You, a part-time dining service worker in your college ' s dining hall,
is now confused with a new problem:serve as many people as possible.
The issue comes up as people in your college is 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
has prepared F (1 <= f <=) kinds of food and D (1 <= d <= 200)
Kinds of drink. Each kind of food or drink have certain amount, that
is, how many people could the food or drink serve. Besides, you know
There ' re N (1 <= n <=) 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 is served while requirements of the rest of them is unmet.
You should notice this, when one's requirement is unmet, he/she would
Just go away, refusing any service.Input

There is 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 den   OTEs 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
Ideas

First say test instructions, there are f food and d drinks, each food or drink can only be limited, and everyone only enjoy one food and one drink. Now there are n cows, everyone has a list of their favorite types of food and a list of drinks, asking for a few people at the same time to enjoy their favorite food and drinks.

First you need a super origin and a super meeting point.

To make sure that everyone drinks with only one food, the person is removed.

To the source point to each food with an edge, the weight is his number, to drink and meeting point connected, the weight is also the amount, each person with the food and drink he needs to connect, the weight value is 1.

And then it's the maximum flow code .

#include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #define INF
0X3F3F3F3F #define MEM (A, B) memset (A,b,sizeof (a)) using namespace Std;
const int N=1000+50;
const int M=1E6+50;
    struct Node {node () {};
    Node (int tv,int tw,int tnext) {v=tv,w=tw,next=tnext;
    };
int v,w,next;
} E[m];
int First[n],vis[n],dis[n],tot;
    void Add_edge (int u,int v,int W) {E[tot]=node (v,w,first[u]);
    first[u]=tot++;
    E[tot]=node (U,0,first[v]);
first[v]=tot++;
    } int BFS (int s,int t) {mem (vis,0);
    MEM (dis,0);
    queue<int>q;
    Q.push (s);
    Vis[s]=1;
        while (!q.empty ()) {int U=q.front ();
        Q.pop ();
                for (int i=first[u]; ~i; i=e[i].next) {if (!vis[e[i].v]&&e[i].w>0) {
                Vis[e[i].v]=1;
                dis[e[i].v]=dis[u]+1;
            Q.push (E[I].V);
}}} return dis[t]; } int dfs (int u,int t,int flow) {if (u==t) return flow; for (int i=first[u]; ~i, I=e[i].next) {if (dis[e[i].v]==dis[u]+1&&e[i].w>0) {in
            T Dd=dfs (E[i].v,t,min (E[i].w,flow));
                if (DD) {e[i].w-=dd;
                E[I^1].W+=DD;
            return DD;
    }}} dis[u]=0;
return 0;
    } int dinic (int s,int t) {int ans=0,flow;
    while (BFS (s,t)) {while (Flow=dfs (S,t,inf)) Ans+=flow;
} return ans;
    } void Init () {mem (first,-1);
tot=0;
} int a[n],b[n];
Char S[n];
    int main () {int n,f,d;
        while (~SCANF ("%d%d%d", &n,&f,&d)) {init ();
        for (int i=1; i<=f; i++) scanf ("%d", &a[i]);
        for (int i=1; i<=d; i++) scanf ("%d", &b[i]);
            for (int i=1; i<=n; i++) {Add_edge (f+2*i-1,f+2*i,1);
            scanf ("%s", s+1);
          for (int j=1; j<=f; J + +) if (s[j]== ' Y ')          Add_edge (j,f+2*i-1,1);
            } for (int i=1; i<=n; i++) {scanf ("%s", s+1);
        for (int j=1; j<=d; J + +) if (s[j]== ' Y ') Add_edge (f+2*i,f+2*n+j,1);
        } for (int i=1; i<=f; i++) Add_edge (0,i,a[i]);
        for (int i=1; i<=d; i++) Add_edge (2*n+f+i,2*n+f+d+1,b[i]);
    printf ("%d\n", Dinic (0,2*n+f+d+1));
} return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.