Hdoj 4322 Candy maximum cost max flow, "Classic topic" "Maximum benefit of maximum flow maintenance cost"

Source: Internet
Author: User


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


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.



Test Instructions: Assign M sweets to n children. There is a n*m matrix that indicates the relationship between the child and the candy, if the number of column J of Row I is 1, the first child likes the J Candy, and vice versa. It is known that if a child is assigned to the candy he likes then he will get the happiness value of k, whereas only 1 of the happiness value can be obtained. Now give you the happy values that these n children need to meet and ask if you can meet all the needs of your child .


Good classic map, infinite Orz Daniel.


Daniel:

As long as you get sugar there must be 1 happiness, at this point the effect of sugar is equivalent. So just consider the distribution of sugar with special effects.
When the degree of happiness exceeds b[i], the extra part is wasted, in order to make the waste as little as possible, we use the cost flow to control, when the maximum cost of the maximum flow, this is the cost of the highest utilization. In the construction of the map only need to consider special sugar on it, the method of building the map:

source point to each sugar: stream is 1 and the cost is 0. If the sugar has a special effect on a person, the edge: stream is 1 and the cost is 0. People to the meeting point edge: The ultimate degree of happiness does not exceed b[i] situation: Flow to b[i]/k, limit the number of such sugars, the cost is K, because the special effects are all used. Happier than B[i]: flow is 1, limit the flow of not more than b[i], the cost is b[i]% K, because the more out of the happy void. When b[i]% k = = 0 o'clock, such an edge is not necessary. When b[i]% k = = 1 o'clock, then the sugar and ordinary sugar, no need to even edge (in fact, I feel that the statement and treatment here is not very appropriate, although it does not affect the results).


Rationale, the next one of their own after the building map, and Daniel's a little different.


Build: Set up the source, super Sink point sink, with a for the current child needs to meet the happiness value

1,source to each candy edge, capacity 1, cost 0;

2, only consider the special sugar, then for the first I like J Candy, then J Candy to the first person to build the edge, the capacity of 1, the cost is 0;

3, each child to the meeting point to build edge. At this point, we need to classify and discuss "Note that a/k are all integers, so be sure to do your own hand."

(1) A% K = = 0, indicating that the child chooses (a/k) a candy he likes to be satisfied, and the child chooses 1 of his favorite candies, will get K's happiness value. Building Edge Information--Capacity (a/k), the cost is K;

(2) A% K! = 0, indicating that the child chooses (a/k + 1) A candy he likes to meet, at this time we can not only build a capacity of (a/k + 1), the cost of the side of K, if this processing we will enlarge the maximum cost of the flow of the maximum benefit, and finally get a happy value of a-a% K + k > a. So we're going to build a capacity of (a/k), the cost for the side of K, and then build a capacity of 1, which costs a% of the side of K. This will not increase the cost of the final inflow to the meeting point when a special candy is used to satisfy the child's needs, and the resulting happiness value is a-a% k + a% K = A.


Build a chart and run the maximum cost flow once.

The end result: (use Sumflow to record the sum of the happy values that all children need to meet)

The maximum cost cost--the sum of the happy values assigned to a particular sugar when it is fully utilized. If cost >= sumflow means that the condition has been met.

Maximum flow flow--in order to achieve the amount of sugar used in such a degree.

So we still have m-flow number of sugar is used as ordinary sugar, as long as M-flow >= Sumflow-cost, can meet the conditions.


AC Code:


#include <cstdio> #include <cstring> #include <queue> #include <algorithm> #define MAXN 50# Define MAXM 5000#define INF 0x3f3f3f3fusing namespace std;struct edge{int from, to, cap, flow, cost, next;}; Edge Edge[maxm];int HEAD[MAXN], Edgenum;int PRE[MAXN], Dist[maxn];bool vis[maxn];int N, M, K;int source, Sink;int Sumflow;    void Init () {edgenum = 0; Memset (Head,-1, sizeof (head));}    void Addedge (int u, int v, int w, int c) {Edge E1 = {u, V, W, 0, C, Head[u]};    Edge[edgenum] = E1;    Head[u] = edgenum++;    Edge E2 = {V, u, 0, 0,-C, Head[v]};    Edge[edgenum] = E2; HEAD[V] = edgenum++;}    void Getmap () {Source = 0, sink = N + M + 1;    int A;    Sumflow = 0;        for (int i = 1; I <= N; i++) {scanf ("%d", &a);        Sumflow + = A; Addedge (i, Sink, a/k, K);//can choose a/k his favorite candy if (a% K > 1)//guarantee that there will be no excess cost flow into the meeting point to maintain maximum flow cost of maximum benefit Addedge (    I, sink, 1, a% K); } for (int i = 1; I <= M; i++)//source point from each candy Addedge (sOurce, I+n, 1, 0);            for (int i = 1; I <= N; i++) {for (int j = 1; J <= M; j + +) {scanf ("%d", &a);        if (a)//based on preferred relationship Addedge (J+n, I, 1, 0);    }}}bool SPFA (int s, int t) {queue<int> Q;    memset (Dist,-inf, sizeof (Dist));    Memset (Vis, false, sizeof (VIS));    memset (Pre,-1, sizeof (pre));    Dist[s] = 0;    Vis[s] = true;    Q.push (s); while (!        Q.empty ()) {int u = q.front ();        Q.pop ();        Vis[u] = false;            for (int i = head[u]; i =-1; i = Edge[i].next) {Edge E = Edge[i]; if (Dist[e.to] < Dist[u] + e.cost && e.cap > E.flow) {dist[e.to] = Dist[u] + e.cos                T                Pre[e.to] = i;                    if (!vis[e.to]) {vis[e.to] = true;                Q.push (e.to); }}}} ' return pre[t]! =-1;} void MCMF (int s, int t, int &cost, int &Amp;flow) {cost = flow = 0;        while (SPFA (s, t)) {int Min = INF;            for (int i = pre[t]; i =-1; i = pre[edge[i^1].to]) {Edge E = Edge[i];        min = min (min, e.cap-e.flow);            } for (int i = pre[t]; i =-1; i = pre[edge[i^1].to]) {edge[i].flow + = Min;            Edge[i^1].flow = Min;        Cost + = Edge[i].cost * Min;    } flow + = Min;    }}int Main () {int T, k = 1;    scanf ("%d", &t);        while (t--) {scanf ("%d%d%d", &m, &n, &k);        Init ();        Getmap ();        int cost, flow;        MCMF (source, sink, cost, flow);        The amount of traffic that runs out is the number of special candies//The maximum cost is to use these candies to get the maximum happiness value of printf ("Case #%d:", k++);        if (Cost >= sumflow | | sumflow-cost <= m-flow) printf ("yes\n");    else printf ("no\n"); } return 0;}


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

Hdoj 4322 Candy maximum cost max flow, "Classic topic" "Maximum benefit of maximum flow maintenance cost"

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.