[Bzoj 1221] [HNOI2001] Software development "Cost Flow | | Three points "

Source: Internet
Author: User
Tags cmath

Title Link: BZOJ-1221

Problem analysis

Algorithm one: Minimum cost maximum flow

First, this is a classic network flow problem. Create two nodes per day, one I means using towels, and one I ' means the towel used this day.

Then I to T even Ai (the number of towels needed for the first day). From S to I ' Even AI, so the new used towel this day is AI.

Then I ' can be connected to (i+1) ', said to stay until the next day to deal with, I can also flow to i+p+1 and i+q+1, indicating that after washing again use, these two kinds of edge is a cost.

There is a new purchase of towels, from S to I company, the cost is to buy new towels.

This solves the problem by using the minimum cost maximum flow.

Algorithm 2:3 points

However, this topic has a magical approach, much faster than the solution of the cost flow.

I found that the code of FAEBDC god Ben in the submission record was extremely fast, so I consulted him and told me that the problem was one of training team's work. The data range of the topic is n <= 10^5 ... I can only orzzz.

In the light of FAEBDC god Ben, I finally indefinitely write this three-point code.

First of all, if you have decided how many towels to buy, it is always best to buy these towels first. Then the next day must first use unused, and then use less expensive washing method, and then use a lot of washing methods.

In this way, using a single queue, O (n) calculates the minimum cost of buying X-bar towels, which is recorded as f (x).

And then.. The point is.. It can be analyzed that f (x) is a single-valley function that can be divided by three x. (How to analyze it = = FAEBDC God Ben explained a bit and then I was too weak to understand ...

Then you can have three happy points.

Code

Cost Stream Code:

#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <cmath > #include <algorithm> #include <queue>using namespace Std;const int maxn = + 5, INF = 999999999;inline int gmin (int a, int b) {return a < b a:b;} inline int gmax (int a, int b) {return a > b a:b;} int n, p, Q, F, FP, Fq, S, T, Tot, Mincost, maxflow;int D[MAXN * 2];bool inque[maxn * 2];struct edge{int u, V, W, Ct; Edge *next, *other;} E[MAXN *, *p = E, *POINT[MAXN * 2], *PRE[MAXN * 2]; inline void Addedge (int x, int y, int z, int k) {Edge *q = ++p; ++p; P-u = x; P-v = y; P-w = z; P--Ct = k; P-Next = point[x]; POINT[X] = P; P, other = Q; Q-u = y; Q-v = x; Q-W = 0; Q-Ct =-K; Q-Next = Point[y]; Point[y] = Q; Q, other = P; }queue<int> Q;bool Found () {memset (d, 0x7f, sizeof (d)), memset (inque, 0, sizeof (inque)); Q.empty ()) Q.pop ();d [S] = 0; Inque[s] = true; Q.push (S); int x;while (! Q.empty ()) {x = Q.front (); INQUE[X] = false; Q.pop (); for (Edge *j = point[x]; j; j = J-Next) if (j, W && D[x] + j, Ct < D[j, V]) {d[j-&G T V] = d[x] + J-Ct; Pre[j v] = j;if (! Inque[j, V]) {inque[j-v] = true; Q.push (J-v);}} return d[t] < INF;} void Augment () {int flow = Inf;for (Edge *j = pre[t]; j; j = pre[j-u]) Flow = Gmin (Flow, J-W); for (Edge *j = Pr E[T]; J j = pre[j-u]) {j, W-= Flow;j, other w + = Flow;} Maxflow + = Flow; Mincost + = Flow * D[t];} int main () {scanf ("%d%d%d%d%d%d", &n, &p, &q, &f, &AMP;FP, &AMP;FQ); int Num; Tot = n * 2; S = ++tot; T = ++tot;for (int i = 1; I <= n; ++i) {scanf ("%d", &num); Addedge (S, I, Num, f); Addedge (i, T, Num, 0); Addedge (S, n + i, Num, 0);} for (int i = 1; i < n; ++i) {Addedge (n + i, n + i + 1, INF, 0), if (i + p + 1 <= N) Addedge (n + i, i + p + 1, INF, FP) if (i + q + 1 <= N) Addedge (n + i, i + q + 1, INF, FQ);} while (Found ())Augment ();p rintf ("%d\n", Mincost); return 0;} 

Three-minute code:

#include <iostream> #include <cstdlib> #include <cstring> #include <cmath> #include <cstdio  > #include <algorithm>using namespace std;inline void Read (int &num) {char c = getchar (); while (C < ' 0 ' | | c > ' 9 ') c = GetChar (); Num = C-' 0 '; c = GetChar (); while (c >= ' 0 ' && C <= ' 9 ') {num = num * + C-' 0 '; c = GetChar ();}} const int MAXN = + 5, INF = 999999999;inline int gmin (int a, int b) {return a < b? A:b;} int n, p, Q, F, FP, Fq, SumA, Head, Tail;int A[MAXN], q[maxn][2], ans;int Calc (int x) {int ret, Cnt, Rest; Head = 1; Tail = 0; Rest = X;ret = 0;for (int i = 1; I <= n; ++i) {if (i-p-1 > 0) {q[++tail][0] = i-p-1; Q[TAIL][1] = a[i-p-1];} if (rest >= a[i]) Rest-= A[I];ELSE{CNT = A[i]-rest;  Rest = 0;while (cnt > 0 && Head <= Tail) {if (q[head][0] <= i-q-1) {if (q[head][1] > cnt) {ret = cnt * FQ; Q[HEAD][1]-= Cnt; Cnt = 0;} Else{ret + = q[head][1] * FQ; Cnt-= Q[head][1];++head;}} Else{if (q[tAIL][1] > cnt) {ret = cnt * FP; Q[TAIL][1]-= Cnt; Cnt = 0;} Else{ret + = q[tail][1] * FP; Cnt-= Q[tail][1];--tail;}}} if (Cnt > 0) return INF;}} RET + = f * x;return ret;} int main () {scanf ("%d%d%d%d%d%d", &n, &p, &q, &f, &AMP;FP, &AMP;FQ); for (int i = 1; I <= n; ++i) {Read (A [i]); SumA + = A[i];} int L = 1, R = SumA, Mid1, Mid2;while (r-l >= 3) {mid1 = L + (r-l)/3;mid2 = R-(r-l)/3;if (Calc (MID1) < Ca LC (MID2)) R = Mid2-1;else L = mid1 + 1;} ans = inf;for (int i = l; I <= R; ++i) Ans = Gmin (Ans, Calc (i));p rintf ("%d\n", Ans); return 0;}

  

[Bzoj 1221] [HNOI2001] Software development "Cost Flow | | Three points "

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.