Poj2349--arctic Network

Source: Internet
Author: User
Tags cmath

Arctic Network
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10955 Accepted: 3592

Description

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.

Input

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).

Output

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

Source

Waterloo Local 2002.09.28

Method 1: d+ and check the set, if the number of connected blocks is less than or equal to s, then D may be smaller, or larger, POJ time-consuming about 800 ms

/************************************************************************* > File Name:POJ2349.cpp > Author: ALex > Mail: [email protected] > Created time:2015 January 25 Sunday 13:33 36 seconds ******************************* /#include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm>using namespace Std;int father[555];struct node{ int x, y;} Point[555];int find (int x) {if (father[x] = =-1) {return x;} return father[x] = find (Father[x]);} Double Dist (Node A, Node B) {return sqrt ((double) ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));} int main () {int t;scanf ("%d", &t), while (t--) {int S, n;scanf ("%d%d", &s, &n);d ouble L = 0;double r = 20000;dou ble mid;for (int i = 1; I <= n; ++i) {scanf ("%d%d", &point[i].x, &poinT[I].Y);} Double Ret;while (l + 1e-6 < r) {int ans = N;memset (father,-1, sizeof (father)), Mid = (L + R)/2;for (int i = 1; I &lt ; = N; ++i) {for (int j = i + 1; j <= N; ++j) {if (Dist (point[i], point[j]) + 1e-6 <= mid) {int u = find (i); int v = find (j); if (U = v) {Father[u] = V;--ans;}}}} if (ans <= s) {r = Mid;ret = Mid;} Else{l = mid;}} printf ("%.2f\n", ret);} return 0;}

Method Two: MST, which marks the edges added to the MST, removes the largest s-1 bar, and the largest of the remaining edges is D, which takes about dozens of MS


/************************************************************************* > File Name:poj2349.cpp > Author: ALex > Mail: [email protected] > Created time:2015 January 25 Sunday 13:08 14 seconds ******************************* /#include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm>using namespace std;double mat[555][555];d ouble Lowc[555];int Pre[555];bool vis[555];d ouble dist[555];int N, s;struct node{int x, y;} Point[555];void Prim (int n, int s) {memset (Vis, 0, sizeof (VIS)), for (int i = 2; I <= n; ++i) {lowc[i] = Mat[1][i];p re[i ] = 1;}  PRE[1] = -1;vis[1] = 1;lowc[1] = 0;int cnt = 0;for (int i = 2; I <= n; ++i) {Double minc = 0x3f3f3f3f;int p;for (int j = 1; J <= N; ++J) {if (!vis[j] && minc > Lowc[j]) {minc = Lowc[j];p= j;}} dist[cnt++] = minc;vis[p] = 1;for (int j = 1; j <= N; ++j) {if (!vis[j] && mat[p][j] < Lowc[j]) {Lowc[j] = Mat [P] [j];p re[j] = p;}}} Sort (dist, dist + cnt);p rintf ("%.2f\n", Dist[cnt-s]);} Double Dist (Node A, Node B) {return sqrt (double ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));} int main () {int t;scanf ("%d", &t), while (t--) {scanf ("%d%d", &s, &n), for (int i = 1; I <= n; ++i) {scanf ("%d% D ", &point[i].x, &AMP;POINT[I].Y);} Memset (Mat, 0x3f3f3f3f, sizeof (MAT)); for (int i = 1; I <= n; ++i) {for (int j = i + 1; j <= N; ++j) {Mat[i][j] = mat[ J][i] = Dist (Point[i], point[j]);}} Prim (n, s);} return 0;}


Poj2349--arctic Network

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.