UVA 10369-arctic Network (minimum spanning tree)
Topic links
The main idea: the Arctic has n outposts, if the two outposts have installed wireless satellites, then they can communicate with each other, but now only M-1 Sentinel installed wireless satellite, the rest of the N-M + 1 waypoints need to install cable, cable cost and distance in proportion, So you are asked to find the smallest d (the distance between two outposts), which enables communication between any outposts.
Problem solving: The smallest d, which is the distance between the minimum two points satisfying the condition. With Kruskal do, and then already have m-1 point already can communicate with each other, then only need to connect n-m + a point, there is n-m edge, from small to large sort edge, as long as find the N-m large access edge is the smallest d.
Code:
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace STD;Const intMAXN =505;Const intMAXM =300000;intU[MAXM], V[MAXM], R[MAXM];intP[MAXN];DoubleW[MAXM];structNode {intx, y;} NODE[MAXN]; double Dist (int i, int j) {Doublex = node[i].x-node[j].x;Doubley = node[i].y-node[j].y;return sqrt(x * x + y * y);} void init (int n, int cnt) { for(inti =0; I < n; i++) P[i] = i; for(inti =0; I < CNT; i++) R[i] = i;} int getParent (int x) {return(x = = P[x])? X:P[X] = getParent (P[x]);} int cmp (int i, int j) {returnW[i] < w[j];} Double kruskal (int n, int cnt, int m) {init (n, CNT); Sort (R, R + CNT, CMP);intflag = n-m;DoubleAns for(inti =0; I < CNT; i++) {intP = GetParent (U[r[i]);intQ = GetParent (V[r[i]);if(P = = Q)Continue;if(flag--) {ans = w[r[i]]; P[P] = Q; }Else returnAns }returnAns;} int main () {intTscanf("%d", &t);intN, M; while(t--) {scanf("%d%d", &m, &n); for(inti =0; I < n; i++)scanf("%d%d", &node[i].x, &NODE[I].Y);intpos =0; for(inti =0; I < n; i++) for(intj = i +1; J < N; J + +) {U[pos] = i; V[pos] = j; w[pos++] = Dist (i, j); }printf("%.2lf\n", Kruskal (n, POS, M)); }return 0;}
UVA 10369-arctic Network (minimum spanning tree)