Very bare template question
Minimum Spanning Tree and secondary Spanning Tree
Code:
# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int n = 110; const int M = 310; const int INF = 100000000; int N, m, map [N] [N], p [N], d [N], F [N] [N], S1, S2, Mark [N]; void prim () {for (INT I = 1; I <= N; ++ I) P [I] =-1, D [I] = inf; int Mi, v; bool vis [N]; memset (VIS, 0, sizeof (VIS); D [1] = 0, S1 = 0; For (INT u = 0; U <n; ++ U) {mi = inf; For (INT I = 1; I <= N; ++ I) if (! Vis [I] & mi> d [I]) MI = d [I], V = I; S1 + = mi; vis [v] = true; for (INT I = 1; I <= N; ++ I) if (! Vis [I] & D [I]> map [v] [I]) d [I] = map [v] [I], P [I] = V ;}} void DFS (INT v) {for (INT u = 1; U <= N; ++ U) {If (! Mark [u] & P [u] = V & U! = V) {mark [u] = true; For (INT x = 1; x <= N; ++ X) if (MARK [x] & U! = X) {f [x] [u] = f [u] [x] = max (F [x] [v], map [u] [v]);} DFS (u) ;}}int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M); For (INT I = 1; I <= N; ++ I) for (Int J = 1; j <= N; ++ J) map [I] [J] = inf; while (M --) {int A, B, C; scanf ("% d", & A, & B, & C); map [a] [B] = map [B] [a] = C ;} prim (); memset (F, 0, sizeof (f); memset (mark, 0, sizeof (Mark); Mark [1] = true; DFS (1); S2 = inf;/* For (INT I = 1; I <= N; ++ I) {for (Int J = 1; j <= N; ++ J) printf ("% d", F [I] [J]); printf ("\ n");} */For (INT I = 1; I <= N; ++ I) for (Int J = I + 1; j <= N; ++ J) {If (J! = P [I] & I! = P [J]) {S2 = (s2 <= S1-f [I] [J] + map [I] [J]? S2: S1-f [I] [J] + map [I] [J]);} printf ("% d \ n", S1, s2 );}}