HDU 3667/2010 Harbin division h Subject fee and traffic are non-linear relations/fee Flow

Source: Internet
Author: User

Question: Modify the question of the general Charge flow: When a route is passed, ai * x ^ 2 (AI is the given coefficient) is required for every X units of traffic ).

At the beginning, I thought it was just a change to the final statistical cost. I saw a sample and found that it was not that simple (PS: check whether the sample can be used before coding every time !), Because it is a square relationship, the next time the traffic increases, it may not be the best! So I thought I could only increase one unit of traffic each time! After each increase of a unit, the cost side of all sides on the path is changed to I ^ 2-(I-1) ^ 2, (First certainly augmented A, then 3A. 5a. 7A .... Since it has been extended for the first time, just use the I-th I-square !) In this way, the question is correct! One commit wa! I scanned the code and found that the cost of the reverse side was not changed! So modify: the essence of the reverse side is to provide the opportunity to regret, and I control the growth of a unit every time! If you regret it once, you must pay the last fee! Therefore, unlike in normal times, the fee for initializing the reverse edge is W (not-W), and the increment value is changed to-1A.-3A.-5A each time. This is a step slower than the forward side! (In fact, this is the essential function of the reverse side, but the cost remains unchanged at ordinary times, so you don't need to do this !) AC after submission! (PS: I feel more clear about the internal logic of the billing flow algorithm)

After reading the online question: the data traffic is <5, so the split of each side is 1, and the fee increases accordingly. Second, the essence is to increase the traffic by 1 each time, which is similar. However, the idea of splitting edges is worth learning!

Additional gains: 1. Pay attention to the data range. Sometimes you can think about algorithms from above. 2: The sample code has been repeated before the code is coded. It may be inspired by ideas!


# Include <cstdio> # include <iostream> # include <queue> # include <cstring> using namespace STD; const int maxv = 200; const int MaxE = 5000*2*2; const int INF = 0x3f3f3f; int nume = 0; int e [MaxE] [5]; int head [maxv]; int n, m, K; void inline Adde (int I, int J, int C, int W, int f) // Add a new parameter F to record the initial fee. Fee W changed {e [nume] [0] = J; E [nume] [1] = head [I]; head [I] = nume; E [nume] [2] = C; E [nume] [3] = W; E [nume ++] [4] = F; E [nume] [0] = I; E [nume] [1] = head [J]; head [J] = nume; E [nume] [2] = 0; E [nume] [3] = W; E [nume ++] [4] = f;} int INQ [maxv]; int pre [maxv]; int PRV [maxv]; int d [maxv]; bool spfa (Int & sum, Int & flow) {int S = 0, T = n + 1; for (INT I = 0; I <= N + 3; I ++) {INQ [I] = 0; d [I] = inf;} queue <int> q; q. push (s); INQ [s] = 1; d [s] = 0; while (! Q. empty () {int cur = Q. front (); q. pop (); INQ [cur] = 0; For (INT I = head [cur]; I! =-1; I = E [I] [1]) {int v = E [I] [0]; if (E [I] [2]> 0 & D [cur] + E [I] [3] <D [v]) {d [v] = d [cur] + E [I] [3]; Pre [v] = I; PRV [v] = cur; If (! INQ [v]) {q. push (V); INQ [v] = 1 ;}}} if (d [T] = inf) return 0; int cur = T; int minf = 1; // increase the value of one unit at a time while (cur! = S) {int Fe = pre [cur]; E [Fe] [3] + = E [Fe] [4] * 2; // key point. E [Fe ^ 1] [3]-= E [Fe] [4] * 2; // This is a step slower than the forward side. (Initial Value Control) cur = PRV [cur];} cur = T; while (cur! = S) {e [pre [cur] [2]-= minf; E [pre [cur] ^ 1] [2] + = minf; cur = PRV [cur];} flow + = minf; sum + = d [T]; //, each time a unit, needless to say, the minimum short circuit is the total cost of return 1 ;} int mincost (Int & flow) {int sum = 0; while (spfa (sum, flow); Return sum;} void Init () {nume = 0; for (INT I = 0; I <= n + 2; I ++) head [I] =-1;} int main () {While (~ Scanf ("% d", & N, & M, & K) {Init (); int A, B, X, C; for (Int J = 0; j <m; j ++) {scanf ("% d", & A, & B, & X, & C); Adde (a, B, c, x, x);} Adde (0, 1, K, 0, 0); Adde (n, n + 1, K, 0, 0 ); // append the edge to determine whether the traffic is full. Int flow = 0; int ans = mincost (flow); If (flow! = K) printf ("-1 \ n"); else printf ("% d \ n", ANS) ;}return 0 ;}



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.