The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver and some outposts will in addition have a satellite channel.
Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two 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 be identical; that is, 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 be at least one communication path (direct or indirect) between every pair of outposts.
Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two 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 be identical; that is, 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 be 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 each test case contains 1 <= S <= 100, the number of satellite channels, and S < P <= 500, the number of outposts. P lines follow, giving the (x,y) coordinates of each outpost in km (coordinates are 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 be specified to 2 decimal points.
1 2 4 0 100 0 300 0 600 150 750
212.13
Description
国防部(DND)要用无线网络连接北部几个哨所。两种不同的通信技术被用于建立网络:每一个哨所有一个无线电收发器,一些哨所将有一个卫星频道。任何两个有卫星信道的哨所可以通过卫星进行通信,而不管他们的位置。同时,当两个哨所之间的距离不超过D时可以通过无线电通讯,D取决于对收发器的功率。功率越大,D也越大,但成本更高。出于采购和维修的方便,所有哨所的收发器必须是相同的;那就是说,D值对每一个哨所相同。你的任务是确定收发器的D的最小值。每对哨所间至少要有一条通信线路(直接或间接)。Input
输入的第一行是测试数据的数量N。每组测试数据的第一行包含卫星频道的数量S(1 < = S < = 100)和哨所的数量P(S < P < = 500)。接下来的P行,给出以公里为单位的每个哨所的坐标(x,y)( 坐标为0到10000之间的整数)。Output
对于每组测试数据,输出一行,输出收发器的D的最小值。精确到小数点后两位。Sample Input
1 2 4 0 100 0 300 0 600 150 750Sample Output
212.13
就是说有S条免费的通道,我们要让这S条通道发挥最大的作用,所以要让着S条通道尽可能的代替大的边,所以就是贪心,但是Kruskal算法正好发挥这种贪心的作用,这里Prim算法就不好想了。
#include <math.h> #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #define inf 0x3f3f3f3f using namespace std; int f[505]; double num[505]; struct node{ int x, y; }s[505]; struct edge{ int x, y; double cost; void Edge(int x1, int y1, double W) { x = x1; y = y1; cost = W; } }e[250050]; double Distance(node a, node b) { return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)); } bool cmp(edge a, edge b) { return a.cost < b.cost; } int getf(int v) { if(f[v] != v) { f[v] = getf(f[v]); } return f[v]; } void init(int n) { for(int i = 1; i <= n; i++) { f[i] = i; } } int main() { int N; int S, P; scanf("%d", &N); while(N--) { int step = 0; scanf("%d %d", &S, &P); init(P); for(int i = 1; i <= P; i++) { scanf("%d %d", &s[i].x, &s[i].y); } for(int i = 1; i <= P; i++) { for(int j = 1; j <= P; j++) { e[step++].Edge(i, j, Distance(s[i], s[j])); } } sort(e, e+step, cmp);//排序 int p = 0; for(int i = 0; i < step; i++) { int t1 = getf(e[i].x); int t2 = getf(e[i].y); if(t1 != t2) { f[t1] = t2; num[p++] = e[i].cost;//每次取值 } } printf("%.2f\n", num[P-S-1]); } return 0; }