HDU 4322 Candy (maximum cost max flow) Classic

Source: Internet
Author: User

CandyTime limit:4000/2000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 1767 Accepted Submission (s): 478


Problem Descriptionthere is n candies and M kids, the teacher would give this N candies to the M kids. The i-th kids for the j-th candy have a preference for like[i][j], if he like the sugar, like[i][j] = 1, otherwise like[i][ J] = 0. If The i-th kids get the candy which he like he'll get K glad value. If He or she does not like it. He'll get only one glad value. We know that the i-th kids would feel happy if he the sum of glad values is equal or greater than b[i]. Can-Tell me-whether reasonable allocate this N-candies, make every kid feel happy.
Inputthe Input consists of several cases. The first line contains a single integer T. The number of test cases.
For each case starts with a line containing three integers N, M, K (1<=n<=13, 1<=m<=13, 2<=k<=10)
The next line contains M numbers which is b[i] (0<=b[i]<=1000). Separated by a single space.
Then there is m*n like[i][j], if the i-th kids like the j-th sugar like[i][j]=1, or like[i][j]=0.

Outputif There has a reasonable allocate make every kid feel happy, output "YES", or "NO".
Sample Input
23 2 22 20 0 00 0 13 2 22 20 0 00 0 0

Sample Output
Case #1: yescase #2: NOHintGive The first and second candy to the first kid. Give the third candy to the second kid. This allocate make all kids happy.

Authorbjtu
Source2012 multi-university Training Contest 3  Test instructions: There are N sweets (numbered 1~n) assigned to M Children (numbered 1~m), if like[I [J]==1 illustrates that I children like to eat the first J candy, if He can get the value K if he gets the first J candy, and if he gets a candy that he doesn't like, it's worth 1. Ask whether each child I get to meet the value >=b[i]. If the output Yes is satisfied, otherwise no. Problem-solving: first, each candy should play the maximum value, so as far as possible to the children like the candy, but the candy may have a lot of children like, that how to divide, we can first give those who get the sum of   value   and b[i] The difference between the largest, so as to make full use of the candy, Then there is the priority of the point, you can use the maximum cost of the maximum flow to do. For each child, the value of the candy you don't like is only 1, so you can deal with it individually. Build: Edge < U, V, cap, cost >,i: I candy, J: J Child 1. <vs,  i, 1, 0  > 2. < I, j+n, 1, 0 >: Condition like[j] [i]==13. < J+n, VT, B[j]/k, K >4. < J+n, VT, 1, b[j]%k > Maximum cost maximum flow run once to count the flow of points that flow through the meeting point VT (that is, the number of sweets each child likes), then the judgment.
/* Minimum cost maximum flow: The maximum cost is only the cost of the reverse, the results can be reversed. Algorithm idea: First use SPFA to find a minimum cost of the extensible flow path, and then update the residual flow network, array simulation queue fast map: adjacency table */#include <stdio.h> #include <string.h> #include <  queue>using namespace Std;const int maxn = 10010;const int MAXM = 100100;const int INF = 1<<30;struct edg{int    To,next,cap,flow;  int cost; Unit price}edg[maxm];int Head[maxn],eid;int PRE[MAXN], COST[MAXN];    Point 0~ (n-1) void init () {eid=0; memset (head,-1,sizeof (Head));}    void addedg (int u,int v,int cap,int cst) {edg[eid].to=v; edg[eid].next=head[u]; edg[eid].cost = CST; Edg[eid].cap=cap; edg[eid].flow=0;    head[u]=eid++; Edg[eid].to=u; EDG[EID].NEXT=HEAD[V];    Edg[eid].cost =-CST; Edg[eid].cap=0; edg[eid].flow=0; head[v]=eid++;}    BOOL Inq[maxn];int q[maxn];bool SPFA (int snode,int enode,int n) {int l=0, r=0;    for (int i=0; i<n; i++) {inq[i]=false; cost[i]=-1; } cost[snode]=0; Inq[snode]=1;    Pre[snode]=-1;    Q[r++]=snode;        while (l!=r) {int u=q[l++];        if (L==MAXN) l=0;        inq[u]=0; for (iNT I=head[u]; I!=-1;            I=edg[i].next) {int v=edg[i].to; if (edg[i].cap-edg[i].flow>0 && cost[v]<cost[u]+edg[i].cost) {//In case of an incremental flow, the minimum cost cost[v] = costs                [U]+edg[i].cost;   Pre[v]=i;                    The Edge if (!inq[v]) {if (R==MAXN) r=0 on the record path;                    Q[r++]=v;                Inq[v]=1;    }}}} ' return Cost[enode]! =-1;    Determine if there is no augmented path}//reverse is the maximum flow, the minimum cost is mincostint mincost_maxflow (int snode,int enode,int& mincost,int n) {int ans=0;        while (SPFA (snode,enode,n)) {int mint=inf; for (int i=pre[enode]; i!=-1; i=pre[edg[i^1].to]) {if (Mint>edg[i].cap-edg[i].flow) mint=edg[i]        . Cap-edg[i].flow;        } Ans+=mint;            for (int i=pre[enode]; i!=-1; i=pre[edg[i^1].to]) {edg[i].flow+=mint; edg[i^1].flow-=mint;        Mincost+=mint*edg[i].cost; }} return ans;  int main () {int t,n,m,k, VS, VT;    scanf ("%d", &t);        for (int cas = 1; CAs <= T; cas++) {scanf ("%d%d%d", &n,&m,&k); vs = 0;        VT = m+n+1;        int ans = 0;        Init ();        for (int i = 1; I <= n; i++) Addedg (VS, I, 1, 0);        int b[20], tmp;            for (int i = 1; I <= m; i++) {scanf ("%d", &b[i]);            TMP = b[i]/k;            ADDEDG (I+N, VT, TMP, K);            if (b[i]%k! = 0) addedg (I+N, VT, 1, b[i]%k);        Ans + = b[i];            } for (int i = 1; I <= m; i++) for (int j = 1; J <= N; j + +) {scanf ("%d", &tmp);         if (TMP) ADDEDG (J, I+n, 1, 0);         } int tn = n, flow[30]={0};         N-= Mincost_maxflow (VS, VT, TMP, VT+1);             for (int i = HEAD[VT]; ~i; i=edg[i].next) {int u = EDG[I].TO-TN;         Flow[u] + = Edg[i^1].flow; } for (int i = 1; I <= m; i++) {tmp = Flow[i];             if (Tmp*k>=b[i]) ans-= b[i];                else{B[i]-= tmp*k;                Ans-= tmp*k;                if (b[i]>n) break;                Ans-= b[i];             N-= B[i];         }} printf ("Case #%d:", CAs);         if (ans>0) printf ("no\n");    else printf ("yes\n"); }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 4322 Candy (maximum cost max flow) Classic

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.