UVA 10369-arctic Network (minimum spanning tree)

Source: Internet
Author: User

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, &AMP;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.