Título: http://acm.hdu.edu.cn/showproblem.php?pid=1875
Análisis: algoritmo kruskal. Primero asegúrese de que cada isla tenga al menos un borde efectivo conectado a otras islas, luego directamente kruskal. (Pero recibí un error muy inesperado. Si uso C ++ 11 en ide, habrá un error mágico, pero después no habrá ningún error. Es posible usar G ++ en vjudge)
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <algorithm> 5 usando el espacio de nombres std; 6 struct edge 7 { 8 int u, v; 9 doble w; 10 } e [ 110000 ]; 11 struct punto 12 { 13 int x, y; 14 } p [ 110 ]; 15 int t, n, f [ 110 ], cnt, tot; 16 dobles ans; 17 int find ( int k) 18 { 19 if (f [k] == k) devuelve k; 20 retorno f [k] = encontrar (f [k]); 21 } 22 doble cmp (borde a, borde b) 23 { 24 return aw < bw; 25 } 26 doble distancia ( int i, int j) 27 { 28 return sqrt ((p [i] .xp [j] .x) * (p [i] .xp [j] .x) + (p [i ] .yp [j] .y) * (p [i] .y- p [j] .y)); 29 } 30 nuloadd ( int u, int v, double w) 31 { 32 e [++ cnt] .u = u; 33 e [cnt] .v = v; 34 e [cnt] .w = w; 35 } 36 int main ( void ) 37 { 38 scanf ( " % d " , & t); 39 while (t-- ) 40 { 41 tot = cnt = 0 ; 42 ans = 0 ; 43 intb = 0 ; 44 scanf ( " % d " , & n); 45 para ( int i = 1 ; i <= n; i ++ ) 46 { 47 scanf ( " % d% d " , & p [i] .x, & p [i] .y); 48 f [i] = i; 49 } 50 para ( int i = 2 ; i <= n; i ++ ) 51 { 52 int p = 0 ; 53 para (int j = 1 ; j <i; j ++ ) 54 { 55 doble d = distancia (i, j); 56 if (d <= 1000 && d> = 10 ) 57 { 58 add (i, j, d); 59 p = 1 ; 60 } 61 } 62 if (p == 0 ) 63 { 64 printf ( " oh! \ N " ); sesenta y cinco b = 1 ; 66 descanso ; 67 } 68 } 69 si (b == 1 ) continúa ; 70 sort (e + 1 , e + 1 + cnt, cmp); 71 para ( int i = 1 ; i <= cnt; i ++ ) 72 { 73 int fu = find (e [i] .u), fv = find (e [i] .v); 74 if (fu == fv) continúa ; 75 f [fu] = fv; 76 ans + = e [i] .w; 77 if (++ tot == n- 1 ) break ; 78 } 79 printf ( " % .1f \ n " , ans * 100 ); 80 } 81 devuelve 0 ; 82 }