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 ());}}