Hdu4292--food (sap+ split)

Source: Internet
Author: User

Problem 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 CER tain kinds of food and drink, and with requirement unsatisfied, go away directly.
you had prepared F (1 <= f <=) kinds of food and D (1 <= d <=) kinds of drink. Each of the kind of food or drink have certain amount, that's, 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 D Rink.
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, and 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 wheth Er 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 wheth Er 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, maximum number of people to be satisfied. * Print a single line with one integer.

Sample Input
4 3 3
1 1 1
1 1 1
Yyn
Nyy
Yny
Yny
Yny
Yyn
Yyn
Nny

Sample Output
3

This principle and poj3281 very similar, I used the sap template just learned, and found the following several problems (I can not believe I actually found):
n is the number of all points, theoretically n=des+1 on the line;
The number of points MAXN and the number of edges is very metaphysical, to my knowledge level do not know what kind of relationship between them, the total number of the beginning of the edge is 4 times times the point, WA, changed to 32 times times over.

#include <iostream> #include <cstring> #include <string> #include <vector> #include <queue
> #include <cstdio> #include <set> #include <cmath> #include <map> #include <algorithm>
#define INF 0x3f3f3f3f #define MAXN 11000 #define MOD 10001 using namespace std; struct E {int to, frm, NXT, cap;}
edge[maxn<<5];
int HEAD[MAXN], E, n, M, Src, des; int DEP[MAXN], GAP[MAXN];
    Gap[x]=y: Indicates that the number of dep[i]=x in the residual network is y void Addedge (int u, int v, int c) {edge[e].frm = u;
    Edge[e].to = v;
    Edge[e].cap = C;
    EDGE[E].NXT = Head[u];
    Head[u] = e++;
    Edge[e].frm = v;
    edge[e].to = u;
    Edge[e].cap = 0;
    EDGE[E].NXT = Head[v];
HEAD[V] = e++;
} int Q[MAXN];
    void BFS (int src, int. des) {memset (DEP,-1, sizeof (DEP));
    memset (Gap, 0, sizeof (GAP));   Gap[0] = 1;
    Description at this time there are 1 dep[i] = 0 int front = 0, rear = 0;
    Dep[des] = 0;
    q[rear++] = des;
    int u, v; while (front! = rear) {u = Q[front++];
        cout<<u<<endl;
        Front = FRONT%MAXN;
            for (int i=head[u]; i!=-1; i=edge[i].nxt) {//cout<<i<<endl;
            v = edge[i].to;
            if (edge[i].cap! = 0 | | dep[v]! =-1) continue;
            q[rear++] = v;
            Rear = rear% Maxn;  ++GAP[DEP[V] = Dep[u] + 1];
Find the number of levels}}} int S[MAXN],CUR[MAXN];
    int SAP () {int res = 0;
    BFS (src, des);
    int top = 0;
    memcpy (cur, head, sizeof (head));
    int u = src, i;
            while (DEP[SRC] < N)//n is the number of nodes {if (U = = des) {int temp = INF, Inser = n; for (i=0; i!=top; ++i) if (temp > edge[s[i]].cap) {temp = Edge
                    [S[i]].cap;
                Inser = i;
                } for (i=0; i!=top; ++i) {edge[s[i]].cap-= temp;
     Edge[s[i]^1].cap + = temp;       } res + = temp;
            top = Inser;
        u = edge[s[top]].frm;
        } if (U! = des && Gap[dep[u]-1] = = 0)//There is a fault, no augmentation road break; for (i = cur[u]; i =-1; i = edge[i].nxt)//traverse the non-traversed node connected to u if (edge[i].cap! = 0 && Dep[u] = = Dep[edge[i]

        . to] + 1)//sequence relationship, find allow break;
            if (i! =-1)//Find Allow arc {cur[u] = i;
        s[top++] = i;//join path Stack u = edge[i].to;//Find Next node} else//No allowed path, modify label the current point's label is 1 more than the number of points connected to it
            {int min = n;
                    for (i = head[u]; i =-1; i = EDGE[I].NXT)//Find v dep[v] smallest point {if (Edge[i].cap = = 0) connected to u)
                Continue
                    if (min > dep[edge[i].to]) {min = dep[edge[i].to];          Cur[u] = i;          The minimum label is the newest allowable arc}}--gap[dep[u]]; Dep[u] The number of changes, so repairChange gap ++gap[dep[u] = min + 1]; Set Dep[u] to min (Dep[v]) + 1, while modifying the corresponding gap[] if (U = src)//The point non-source point && allow arc starting with U does not exist, back point u = EDG
        E[S[--TOP]].FRM;
}} return res;
    } int main () {int n,d,f,w;
        while (~SCANF ("%d%d%d", &n,&f,&d)) {//1~f,f+1~f+n,f+n+1~f+n+n,f+n+n+1~f+n+n+d src=0;
        des=f+n+n+d+1; n=des+1;
        There are also source points, meeting point e=0;
        memset (head,-1,sizeof (head));
            for (int i=1;i<=f;++i) {scanf ("%d", &w);
        Addedge (SRC,I,W);
            } for (int i=1;i<=d;++i) {scanf ("%d", &w);
        Addedge (F+N+N+I,DES,W);
        } for (int i=1;i<=n;++i) {Addedge (f+i,f+n+i,1);
        } string op;
            for (int i=1;i<=n;++i) {cin>>op; for (int j=0;j<f;++j) {if (op[j]== ' Y ') {addEdge (j+1,f+i,1);
            }}} for (int i=1;i<=n;++i) {cin>>op; for (int j=0;j<d;++j) {if (op[j]== ' Y ') {Addedge (f+n+i,f
                +n+n+j+1,1);
        }}} int Ans=sap ();
    printf ("%d\n", ans);
} 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.