The most Standard Minimum Cut Algorithm Application question.
The core idea is to scale down edges: First narrow down the biggest edges, then narrow down the secondary edges.
Basic algorithm: Prime Minimum Spanning Tree Algorithm
However, the data tested in this question seems strange. The running time of the same algorithm varies greatly, and the same code is replaced by wa. The probability of system errors is very small. Is it difficult to test the system?
I am too lazy to continue to test the system for this question. The algorithm is correct, AC.
# Include <stdio. h> # include <string. h> # include <limits. h> const int max_n = 500; int n, m, A, B, C, S, T; int gra [max_n] [max_n], DIS [max_n]; bool vis [max_n], delver [max_n]; inline int min (int A, int B) {return a <B? A: B;} int search (INT v) // v calculates the remaining number of vertices {memset (VIS, 0, sizeof (VIS); memset (DIS, 0, sizeof (DIS); int curmax = 0, cur = 0; S = 0, T = 0; For (INT I = 1; I <v; I ++) {curmax = 0; For (Int J = 1; j <n; j ++) {If (! Vis [J] &! Delver [J]) dis [J] + = gra [cur] [J];} For (Int J = 1; j <n; j ++) {If (! Vis [J] &! Delver [J] & dis [J]> curmax) {curmax = dis [J]; cur = J ;}} vis [cur] = true; if (t = cur) return 0; // the graph is not connected and the cycle is terminated early. The cut point is 0 s = T; t = cur; // The final and penultimate vertices are obtained for the purpose of shrinking the image} return curmax;} // core idea: First narrow down the biggest edge and then narrow down the second largest edge, so narrow down int stoer_wagner () {memset (delver, 0, sizeof (delver); int mincut = int_max; For (INT v = N; V> 1; V --) // N-1 side, current V point {mincut = min (mincut, search (v); If (mincut = 0) return 0; // a little optimization, early termination of delver [T] = true; fo R (INT I = 0; I <n; I ++) if (! Delver [I]) gra [s] [I] = gra [I] [s] + = gra [T] [I];} return mincut = int_max? 0: mincut; // returns 0} int main () {While (~ Scanf ("% d", & N, & M) {memset (GRA, 0, sizeof (GRA); For (INT I = 0; I <m; I ++) {scanf ("% d", & A, & B, & C ); gra [B] [a] = gra [a] [B] + = C;} printf ("% d \ n", stoer_wagner ();} return 0 ;}