Hdoj 2282 Chocolate "minimum charge maximum Flow"

Source: Internet
Author: User



ChocolateTime limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 565 Accepted Submission (s): 281


Problem descriptionlethe loves eating chocolates very much. In Lethe's birthday, she good friend Echo brings N boxes to hers, and makes the boxes on the circle. Furthermore, Echo tells Lethe that there is many chocolates in the boxes, and the total number of chocolates doesn ' t exce Ed N. Also, Echo wants Lethe to displace the chocolates in such a-on-the-in-each-box remains no more than one chocolate. In one move she can shift one chocolate from the current box to its neighboring box. (Each box has a neighboring boxes). Can you tell Lethe the minimum number of move to achieve this goal?
Inputthere is multi-cases (the total number of cases won ' t exceed 20). First line is a integer N (1<=n<=500), the total number of boxes. Then N lines follow, each line includes a number, indicates the number of chocolates in the box.
Outputoutput the minimum number of move.
Sample Input
101330020000

Sample Output
9


Dizzy, at noon to see a question, between the maximum flow and the cost of the flow between the long time, said the code has been changing ... Finally determined to be the cost of the flow, but think of a full 1 hours to build a diagram, drunk.

on the Internet to see Daniel--God horse limit limit capacity, but also according to the Division of the classification of the relationship to ensure the maximum efficiency of the cost ... All kinds of divine thinking, the brain is a bit unbearable.   quickly change the question to do, this way to stay tomorrow Ko off.


Test instructions: There are n a circle of boxes (box 1 and Box N-phase), initially each box will have a certain amount of chocolate. It is known that a box can exchange chocolates with two boxes adjacent to it, counting operations once per exchange. Ask you how many operations you need at least to make a maximum of one chocolate per box.


the question did not ask whether it would succeed. If you ask, simply record the minimum feasible flow to the sink point and the maximum feasible flow, and judge that the maximum flow out of the last run is not within this range.  The cost stream and the KM can be written to fill the KM algorithm.


The simple question, the thought is not specific said. Next Build Map

Build: Set Super source point sources, super sink point sink

1,source to each box, the capacity of the box has the number of chocolates, the cost is 0;

2, each box to sink, capacity of 1, indicating that each box can not exceed 1 chocolate, the cost is 0;

3, each box to the adjacent box to build the edge, the capacity of infinity, that can be unlimited transfer, the cost of 1.

Last run a fee stream.


AC Code:


#include <cstdio> #include <cstring> #include <queue> #include <algorithm> #define MAXN 600# Define MAXM 10000#define INF 0x3f3f3f3fusing namespace std;struct edge{int from, to, cap, flow, cost, next;}; Edge Edge[maxm];int HEAD[MAXN], Edgenum;int DIST[MAXN], Pre[maxn];bool vis[maxn];int n;int source, sink;void Init () {ED    Genum = 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+1;    int A;        for (int i = 1; I <= N; i++) {scanf ("%d", &a);        Addedge (source, I, a, 0);        Addedge (i, sink, 1, 0);            if (i = = 1)//1 and N should pay attention to {Addedge (1, N, INF, 1);        Addedge (1, 2, INF, 1);            } else if (i = = N) {Addedge (n, N-1, INF, 1); Addedge (N, 1, INF, 1);            } else {Addedge (i, I-1, INF, 1);        Addedge (i, I+1, INF, 1);    }}}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) {cost = 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;        }}}int Main () {while (scanf ("%d", &n)! = EOF) {init ();        Getmap ();        int cost;        MCMF (source, sink, cost);    printf ("%d\n", cost); } return 0;}



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

Hdoj 2282 Chocolate "minimum charge maximum 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.