Use kruscal to find the Minimum Spanning Tree. If the cost of creating a route exceeds the cost of creating an airport, remove the edge (this is critical );
# Include <cstdio> # include <string. h ># include <algorithm> using namespace STD; const _ int64 n = 10002; const int INF = 9999999999; struct node {_ int64 U, V; _ int64 cost;} A [n * 10] ;__ int64 Fa [N], CON [N], n, m, Cs; bool vis [N]; void Init () {memset (VIS, false, sizeof (VIS); For (_ int64 I = 0; I <n; I ++) {Fa [I] = I ;}} bool CMP (node A, Node B) {return. cost <B. cost;} int find (int x) {return x = Fa [x]? X: Fa [x] = find (Fa [x]);} int main () {/* freopen ("1.txt"," r ", stdin); freopen (" 2.txt ", "W", stdout); */INT t; scanf ("% d", & T); For (INT I = 1; I <= T; I ++) {Init (); printf ("case % d:", I); scanf ("% d", & N, & M, & CS ); for (Int J = 1; j <= m; j ++) {scanf ("% d % LLD", & A [J]. u, & A [J]. v, & A [J]. cost);} Sort (a + 1, A + m + 1, CMP); _ int64 con = 0, CNT = 0, Res = 0; for (Int J = 1; j <= m; j ++) {int xx = find (A [J]. u), YY = find (A [J]. v); If (XX! = YY & A [J]. cost <CS) {Fa [XX] = YY; con + = A [J]. cost; CNT ++ ;}}if (CNT = N-1) {printf ("% LLD 1 \ n", con + CS );} else {// printf ("n = % d \ n", n); printf ("% LLD", (con + (n-CNT) * cs )); printf ("% LLD \ n", N-CNT) ;}return 0 ;}/ * 5 3 100 1 2 3 2 4 1 3 5 2 */