UVA 10369 (for K-long side, Kruskal algorithm template)

Source: Internet
Author: User



Arctic Networks

Time Limit: MS The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Different communication technologies is to being used in establishing the Network:every outpost would have a radio trans Ceiver and some outposts would in addition has a satellite channel.

Any-outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, outposts can communicate by radio only if the distance between them does not exceed D, which depends of the Power of the transceivers. Higher power yields higher D but costs more. Due to purchasing and maintenance considerations, the transceivers at the outposts must is identical; That's, the value of D is the same for every pair of outposts.

Your job is to determine the minimum D required for the transceivers. There must is at least one communication path (direct or indirect) between every pair of outposts.

The first line of input contains N, the number of test cases. The first line of all test case contains 1 <= s <=, the number of satellite channels, and S < P <=, t He number of outposts. P lines follow, giving the (x, y) coordinates of each outpost in km (coordinates is integers between 0 and 10,000). For each case, output should consist of a single line giving the minimum D required to connect the network. Output should is specified to 2 decimal points.

Sample Input
12 40 1000 3000 600150 750
Sample Output
212.13



Main topic:

There are P sites and s satellite and unlimited length of the wireless network, to the site, the distance between the two places do not have any requirements, wireless network in the distance, the more costs, the cost of the price in accordance with the longest wireless network set up the length of the road to calculate, to find the minimum charges.

Problem Solving Ideas:

Kruskal algorithm template, the edge according to from the big to the small arrangement, section p-s is the request d.


Code:

#include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <string > #define Maxd 510using namespace Std;int p[maxd], R[maxd * Maxd], U[maxd * Maxd], V[maxd * Maxd];d ouble X[maxd], Y[maxd ], W[maxd * Maxd], ans;int N, S, P, T, NX, Ny;int find_set (int x) {return p[x] = = X ×: (P[x] = Find_set (p[x]));} int cmp (const void *_p, const void *_Q)//from large to small rows.    {int *p = (int *) _p;    int *q = (int *) _q; return w[*p] > w[*q]? 1:-1;}    void Init () {scanf ("%d%d", &s, &p);    for (int i = 0; i < P; i + +) scanf ("%lf%lf", &x[i], &y[i]);    t = 0;            for (int i = 0, i < p; i + +) for (int j = i + 1; j < P; J + +) {U[t] = i;            V[t] = j;        W[t++]=sqrt ((X[i]-x[j]) * (X[i]-x[j]) + (Y[i]-y[j]) * (Y[i]-y[j]));    }}void Kruskal ()//combine and check set {ans = 0.0;    for (int i = 0; i < P; i + +) p[i] = i;    for (int i = 0; i < T; i + +) r[i] = i; Qsort (r, T, sizeof(R[0]), CMP);    int cnt = 0;        for (int i = 0; i < T; i + +) {int e = r[i];        NX = Find_set (U[e]);        NY = Find_set (V[e]);            if (NX! = NY) {ans = w[e];            P[NX] = NY;        CNT + +;    } if (cnt = = p-s) break;    }}int Main () {scanf ("%d", &n);        while (N-) {init ();        Kruskal ();    printf ("%.2f\n", ans); } return 0;}


UVA 10369 (for K-long side, Kruskal algorithm template)

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.