Link: HDU 1879
The paths of this question are divided into the repaired and unrepaired ones. Therefore, you only need to change the cost of the repaired paths to 0, which is converted into the general minimum spanning tree question.
# Include <cstdio> # include <algorithm> using namespace STD; int f [105], n, m; struct Stu {int A, B, C;} t [5500]; int CMP (struct Stu X, struct Stu y) {return X. c <Y. c;} int find (int x) {If (X! = F [x]) f [x] = find (F [x]); Return f [X];} int KRUS () {int I, K = 0, S = 0, x, y; for (I = 1; I <= m; I ++) {x = find (T [I]. a); y = find (T [I]. b); If (X! = Y) {S + = T [I]. c; k ++; If (k = N-1) break; F [x] = y;} return s;} int main () {int I, S, Z; while (scanf ("% d", & N )! = EOF) {If (n = 0) break; M = N * (n-1)/2; for (I = 1; I <= m; I ++) {scanf ("% d", & T [I]. a, & T [I]. b, & T [I]. c, & Z); If (Z = 1) // change the cost of the repaired road to 0 T [I]. C = 0 ;}for (I = 1; I <= N; I ++) f [I] = I; sort (t + 1, t + 1 + m, CMP); s = KRUS (); printf ("% d \ n", S);} return 0 ;}