Poj 3801 HDU 3157 crazy circuits-a source sink has the upper and lower bounds of the smallest stream

Source: Internet
Author: User
/* HDU 3157poj 3801 question: a circuit board with N wiring bars (Number 1 ~ N) there are also two power supply terminals +-and then the terminals and the minimum current of M parts are given to find a total current that can make all parts work normally. Then the output impossible is actually a lower bound with a source sink. minimum flow problem handling: 1. construct an additional network 2. find the maximum stream for SS and TT (the full stream of SS and TT has a solution) 3. if there is a solution, find the maximum stream for S and T, and the source sink has the upper and lower bounded minimum flow problem is: 1. create an additional network (without adding [t, s] edges) 2. find the maximum stream for SS and TT 3. add the [t, s] side 4. find the maximum stream for SS and TT 5. if the SS and TT streams are full, the [t, s] traffic is the smallest stream. Code Most of them are extracted from other questions, and comments are messy */# include <stdio. h> # include <string. h> # define INF 0x7fffffffstruct edge // edge {int U, V, F, next, B, c; // Number of the bottom edge of the stream at the front node and the back node of the edge} e [1500]; int head [70], in [70], S, t, SS, TT, Yong, sum; int n, m; void INI () {memset (Head,-1, sizeof (head); Yong = 0; memset (in, 0, sizeof (in); s = 0, T = n + 1, SS = t + 1, TT = SS + 1; // sum = 0;} void Adde (int from, int to, int Xia, int Shang) // Add edge {// Add edge e [Yong]. U = from, E [Yong]. V =, E [Yong]. F = Shang-xia, E [Yong]. B = Xia, E [Yong]. C = Shang; E [Yong]. next = head [from], head [from] = Yong ++; // Add its return edge e [Yong] at the same time. U = To, E [Yong]. V = from, E [Yong]. f = 0, E [Yong]. B = Xia, E [Yong]. C = Shang; E [Yong]. next = head [to], head [to] = Yong ++;} void build () {int I; for (I = 0; I <= T; ++ I) {If (in [I]> 0) Adde (SS, I, 0, in [I]); else {Adde (I, TT, 0, -In [I]); sum + = (-in [I]) ;}} int d [1000], num [1000]; int min (int, int B) {return a <B? A: B;} int sap_gap (int u, int F, int S, int t) // recursive sap {If (u = T) return F; int I, V, mind = T, last = F, cost; for (I = head [u]; I! =-1; I = E [I]. next) {v = E [I]. v; int flow = E [I]. f; If (flow> 0) // when writing a reference template, the flow is written as f {If (d [u] = d [v] + 1) {cost = sap_gap (v, min (last, flow), S, T); e [I]. f-= cost; E [I ^ 1]. F + = cost; last-= cost; If (d [s]> = t + 1) return F-last; If (last = 0) break ;} if (d [v] <mind) Mind = d [v] ;}} if (last = f) {-- num [d [u]; if (Num [d [u] = 0) d [s] = t + 1; d [u] = mind + 1; ++ num [d [u];} return F-last;} int max_f (int s, int t) {int f = 0; memset (D, 0, size Of (d); memset (Num, 0, sizeof (Num); For (Num [s] = t + 1; d [s] <t + 1 ;) F + = sap_gap (S, INF, S, T); Return F;} int main () {int I, dat, U, V, F1, F2, P; char from [10], to [10]; while (scanf ("% d", & N, & M), N + M) {INI (); for (I = 1; I <= m; ++ I) {scanf ("% S % d", from, to, & dat ); if (from [0] = '+') u = s; else sscanf (from, "% d", & U ); if (to [0] = '-') V = T; else sscanf (to, "% d", & V); Adde (u, v, dat, INF); in [u]-= dat, in [v] + = dat;} build (); F1 = max_f (SS, TT); P = Yong; Adde (T, S, 0, INF); F2 = max_f (SS, TT); If (F1 + F2! = Sum) printf ("impossible \ n"); else printf ("% d \ n", E [P ^ 1]. f);} 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.