Zoj 2753 min cut (SW)

Source: Internet
Author: User

This is the same as what we have done before. The difference is that we need to determine whether the question is connected.

Code(Be sure to write the code carefully)

# Include <cstdio> # include <cstring> # include <iostream> using namespace STD; const int n = 550; const int INF = 0x3fffffff; int n, m; int V [N], G [N] [N], d [N], Father [N]; int stoer_wagner () {bool vis [N]; int I, j, res = inf; for (I = 0; I <n; ++ I) V [I] = I; while (n> 1) {int maxp = 1, prev = 0; for (I = 1; I <n; ++ I) {d [V [I] = G [V [0] [V [I]; if (d [V [I]> d [V [maxp]) maxp = I;} memset (VIS, 0, sizeof (VIS); vis [V [0] = true; for (I = 1; I <n; I ++) {if (I = N-1) {res = min (Res, d [V [maxp]); For (j = 0; j <n; ++ J) {G [V [Prev] [V [J] + = G [V [J] [V [maxp]; G [V [J] [V [Prev] = G [V [Prev] [V [J];} V [maxp] = V [-- N];} vis [V [maxp] = true; Prev = maxp; maxp =-1; for (j = 1; j <n; ++ J) if (! Vis [V [J]) {d [V [J] + = G [V [Prev] [V [J]; if (maxp =-1 | D [V [maxp] <D [V [J]) maxp = J ;}} return res ;} int find (int x) {return father [x] = x? X: Father [x] = find (father [x]);} int main () {While (scanf ("% d", & N, & M) = 2) {memset (G, 0, sizeof (g); For (INT I = 0; I <= N; ++ I) Father [I] = I; int x, y, z; while (M --) {scanf ("% d", & X, & Y, & Z ); G [x] [Y] + = z; G [y] [x] + = z; int A = find (x); int B = find (y ); if (! = B) Father [a] = B;} int fn = 0; For (INT I = 0; I <n; ++ I) if (find (I) = I) {fn ++; If (FN> 1) break;} If (FN> 1) {printf ("0 \ n"); continue ;} printf ("% d \ n", stoer_wagner ());}}
Related Article

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.