Sgu 176 upstream and downstream minimum streams

Source: Internet
Author: User

There are m pipelines in a network, and some pipelines must be full. The network contains the source vertex labeled 1 and the sink vertex labeled N. Find the smallest stream in the network flow.

Practice: A general upstream/downstream network stream can be used to test the template.

# Include <stdio. h> # include <string. h> # define LMT 200 # define EPS 2e9typedef struct {int U, V;} line; line E [LMT * LMT]; int mat [LMT] [LMT], fmat [LMT] [LMT], Lim [LMT] [LMT], lev[ LMT], Q [LMT]; int n, m, sum; int BFS (INT s, int t) {int I, j, tail, head; memset (lev, 0, sizeof (lev)); lev[ S] = 1; head = tail = 0; Q [tail ++] = s; while (Head <tail) {I = Q [head ++]; for (j = 0; j <= n + 1; j ++) if (fmat [I] [J]> 0 & 0 = lev[ J]) {lev[ J] = lev[ I] + 1; Q [tail ++] = J;} Return lev[ T]! = 0;} int DFS (int s, int t) {int I, j, Top = 0, ret = 0; Q [top ++] = s; while (top> 0) {I = Q [Top-1]; if (I = T) {int min, back; min = EPS; for (I = 1; I <top; I ++) {If (min> fmat [Q [I-1] [Q [I]) {min = fmat [Q [I-1] [Q [I]; back = I ;}} RET + = min; for (I = 1; I <top; I ++) {fmat [Q [I-1] [Q [I]-= min; fmat [Q [I] [Q [I-1] + = min ;} top = back;} else {for (j = 0; j <= n + 1; j ++) if (fmat [I] [J]> 0 & lev[ J] = lev[ I] + 1) {q [top ++] = J; break ;} if (j> n + 1) {[I] = 0; top -- ;}} return ret;} int dinic (INT S, int t) {int ret = 0; while (BFS (S, T )) RET + = DFS (S, T); return ret;} int test (int s, int T, int mid) {int tn; sum = 0; mat [N] [1] = mid; memset (fmat, 0, sizeof (fmat); int I, j; for (I = 1; I <= N; I ++) {tn = 0; For (j = 1; j <= N; j ++) {tn + = lim [J] [I]-lim [I] [J]; fmat [I] [J] = mat [I] [J]-lim [I] [J];} If (TN> 0) {fmat [s] [I] = tn; sum + = tn;} else fmat [I] [T] =-Tn;} return dinic (S, T)> = sum;} in T Main () {int L, R, I, j, c, d, mid, X, ans; scanf ("% d", & N, & M ); memset (MAT, 0, sizeof (MAT); memset (Lim, 0, sizeof (lim); r = 0; L = 0; // here, l must be 0; otherwise, an error may occur. Somehow, the displayed PE for (x = 0; x <m; X ++) {scanf ("% d", & I, & J, & C, & D); R + = C; mat [I] [J] = C; If (d) Lim [I] [J] = C; E [X]. U = I; E [X]. V = J;} ans =-1; while (L <= r) {mid = (L + r)> 1; if (test (0, n + 1, mid) {r = mid-1; ans = mid;} else l = Mid + 1;} If (ANS =-1) {puts ("impossible "); return 0;} e LSE {test (0, n + 1, ANS); printf ("% d \ n", ANS); for (I = 0; I <m; I ++) printf ("% d % C", mat [E [I]. u] [E [I]. v]-fmat [E [I]. u] [E [I]. v], I = S-1? '\ N': '');} 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.