hdu4322 Candy Fee Flow

Source: Internet
Author: User

Test Instructions:

n Candies, M children, to a matrix like[i][j] show that the first child likes the first J Candy. If the child gets the candy he likes, he will increase the K-happiness, get the dislike, and increase by 1. If the child I happiness value is greater than b[i], then he is happy.

Ask, can there be an allocation scheme, make all children happy, have output yes, no no.

Thought: (Not good, please forgive me, Daniel do not spray)

At first, I looked at a Daniel's summary of the network flow modeling, there is a problem with the same name, but it is inside the K is fixed is 2. Inside the practice is done with the pure maximum flow, did not take into account the cost. The map is like this, each sugar as a point with the Edge (s,i,1), each child J seat a point edge (J,T,B[I]/2), if the child J like Candy I is the Edge (i,j,1), and then the maximum flow, to ans,,ans+n>= sum (B[i] ). His consideration is that each sugar is to be assigned to the child at the end, then it will certainly contribute 1 to the total weight, so this 1 is not considered, and finally add N, only consider if the child like the sugar and the extra contribution, the above problem is not a problem. In fact, the idea of the two questions is very similar, hdu4322 should be changed from the above questions, will k into a change, not 2.

At first I got this problem, the idea of directly set up the above problem, want to put the child J as a point to practice the Edge (j,t,b[i]/k), said the child can use b[i]/k block candy to fill the happiness, go side maximum flow, and then use the rest of the N-ans block sugar to fill these children not enough happiness. Later on it is wrong to do so, such as k=3,b[i]%k==2, if the set of the above method, that is, I need to use two sugars to fill the child's happiness, and the two pieces of sugar may have his favorite candy, because the maximum flow when only let it meet the b[i]/k flow. But when you give the child a candy he likes, happiness will exceed 1, but the use of sweets is less, which is more reasonable. So it is wrong to use only the maximum flow. This is the time to think about the need to use the cost stream.

The total cost represents the total contribution to the candy that these children like, each one for a child it likes candy, the cost is k, and each child's happiness reaches b[i], even if more than also by B[i] count, because more than the part of the meaningless. Then, consider in detail, if a child b[i]%k==0 so build Edge (J,T,B[I]%K,K), capacity is b[i]%k, the cost is K, then if b[i]%k!=0?

If ==1, that is, the remaining happiness even if you give his favorite candy effect is the same as ordinary candy, this time there is no need to build a side. If >1, this time will need to build a second side (J,T,1,B[I]%K), the capacity is 1, the cost is b[i]%k, its meaning is to use a favorite candy to fill the parts of the b[i]%k. But then consider a problem, that is, if you set the minimum cost of the maximum flow of the board, it will definitely go to the second side, but this is not right. In this case, it is possible to incur the cost of the side of the k capacity has the surplus, and the second side has been streaming, the reason to think about it. So what we need to do is let it flow at a high cost, so this is the maximum cost of the flow. Only need to take the opposite number of costs, the flow after the reverse is good.

Finally Judge N-ans>=all (B[i])-cost is good, that is, the rest of the No one likes (or by some children like but the child is happy enough to not want) candy is not able to meet the remaining happiness.

Code: Minimum cost flow of the Board Tun

#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include < queue>using namespace Std;const int inf=1<<29;const int maxn=1000;struct node{int v,next,cap,w;}; Node Edge[maxn<<1];int head[maxn];int cnt;int dis[maxn],pe[maxn],pv[maxn];int m,n,k;int cost,flow;int S,T,all;    BOOL Mark[maxn];void init () {memset (head,-1,sizeof (head));    cnt=0; All=0;}    void Add (int a,int b,int c,int d) {edge[cnt].v=b;    Edge[cnt].cap=c;    Edge[cnt].w=d;    Edge[cnt].next=head[a];    head[a]=cnt++;    Edge[cnt].v=a;    Edge[cnt].cap=0;    edge[cnt].w=-d;    EDGE[CNT].NEXT=HEAD[B]; head[b]=cnt++;}    int mindinic () {int Min;    cost=0,flow=0;        while (1) {for (int i=0;i<=t;i++) Dis[i]=inf;        memset (Mark,false,sizeof (Mark));        Queue<int> Q;        Dis[0] = 0;        Q.push (0);        Mark[0]=true;        int cur;            while (!q.empty ()) {cur = Q.front ();            Q.pop (); mark[Cur]=false;                for (int i = head[cur]; i =-1; i = Edge[i].next) {node x=edge[i];                    if (x.cap&& Dis[x.v]>x.w+dis[cur]) {DIS[X.V] = Dis[cur] + x.w;                    Pe[x.v]=i;                    Pv[x.v]=cur;                        if (!MARK[X.V]) {mark[x.v]=true;                    Q.push (X.V);        }}}} if (Dis[t]==inf) return cost;        Min=inf;        for (int j=t; j!=s; j=pv[j]) min=min (MIN,EDGE[PE[J]].CAP);        Flow+=min;        Cost+=dis[t]*min;    for (int j=t; j!=s; j=pv[j]) edge[pe[j]].cap-=min,edge[pe[j]^1].cap+=min;    }}int Main () {int cas=1;    int t,x;    scanf ("%d", &t);        while (t--) {scanf ("%d%d%d", &n,&m,&k);        Init ();        s=0,t=n+m+1;        for (int i=1; i<=n; i++) {Add (s,i,1,0);    }    for (int i=1; i<=m; i++) {scanf ("%d", &x);            All+=x;            Add (n+i,t,x/k,-k);            if (x%k>1) {Add (n+i,t,1,-x%k); }} for (int i=1, i<=m; i++) {for (int j=1; j<=n; J + +) {SC                ANF ("%d", &x);                if (x) {Add (j,n+i,1,0);             }}} int tmp=-mindinic ();//use a favorite candy to fill the happiness if (n-flow>=all-tmp)//The rest of the candy that no one likes can fill the rest of the happiness        printf ("Case #%d:yes\n", cas++);    else printf ("Case #%d:no\n", cas++); } return 0;}


Copyright NOTICE: This article is the original blogger article, reproduced please indicate the source Http://blog.csdn.net/hitwhacmer1

hdu4322 Candy Fee Flow

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.