P1550 [usaco 08oct] Drilling Hole
The question is simple, but it reminds us of the importance of graph creation.
In many cases, you need to add a secondary vertex instead of creating the given information.
However, this question is true.
# Include <cstdio> # include <algorithm> # include <iostream> # include <cstring> Using STD: sort; const int maxn = 500; struct node {int A, B; int val; bool operator <(const node & A) const {return Val <. val ;}}; node line [maxn * maxn]; int f [maxn], tail; int find (int x) {return f [x] = (F [x] = x? X: Find (F [x]);} int main () {int N; scanf ("% d", & N); For (INT I = 1; I <= N; I ++) f [I] = I; int A; For (INT I = 1; I <= N; I ++) {scanf ("% d", & A); line [++ tail]. a = 0; line [tail]. B = I; line [tail]. val = A ;}for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) {scanf ("% d", & A); line [++ tail]. A = I; line [tail]. B = J; line [tail]. val = A;} Sort (LINE + 1, line + 1 + tail); int get = 0, pass = 1, ANS = 0; while (get <n) {int F1 = line [pass]. a, f2 = line [pass]. b; F1 = Find (F1); F2 = find (F2); If (F1! = F2) {Get ++; F [F1] = F2; ans + = line [pass]. val;} Pass ++;} printf ("% d", ANS );}
p1550 [usaco 08oct] Drilling Hole