POJ 2349 Arctic Network (最小生成树)

Arctic Network

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Appoint description: 
System Crawler  (2015-06-01)

Description

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.

Input

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

Output

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.

Sample Input

1
2 4
0 100
0 300
0 600
150 750

Sample Output

212.13

即求最小生成树中倒数第S + 1大的边,注意不能一开始就把最大的S条边合并,因为这些边不一定能取到,要在kruskal里处理。
 #include <iostream>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std; const int SIZE = ;
int FATHER[SIZE];
int N,M,NUM;
struct Node
{
int from,to;
double cost;
}G[SIZE * SIZE];
struct
{
int x,y;
}TEMP[SIZE]; void ini(void);
int find_father(int);
void unite(int,int);
bool same(int,int);
bool comp(const Node &,const Node &);
double dis(int,int,int,int);
double kruskal(void);
int main(void)
{
int t; scanf("%d",&t);
while(t --)
{
scanf("%d%d",&M,&N);
ini();
for(int i = ;i <= N;i ++)
scanf("%d%d",&TEMP[i].x,&TEMP[i].y);
for(int i = ;i <= N;i ++)
for(int j = i + ;j <= N;j ++)
{
G[NUM].from = i;
G[NUM].to = j;
G[NUM].cost = dis(TEMP[i].x,TEMP[j].x,TEMP[i].y,TEMP[j].y);
NUM ++;
}
sort(G,G + NUM,comp);
printf("%.2f\n",kruskal());
} return ;
} void ini(void)
{
NUM = ;
for(int i = ;i <= N;i ++)
FATHER[i] = i;
} int find_father(int n)
{
if(n == FATHER[n])
return n;
return FATHER[n] = find_father(FATHER[n]);
} void unite(int x,int y)
{
x = find_father(x);
y = find_father(y); if(x == y)
return ;
FATHER[x] = y;
} bool same(int x,int y)
{
return find_father(x) == find_father(y);
} bool comp(const Node & a,const Node & b)
{
return a.cost < b.cost;
} double dis(int x_1,int x_2,int y_1,int y_2)
{
return pow(x_1 - x_2,) + pow(y_1 - y_2,);
} double kruskal(void)
{
int count = ;
double ans = ,temp;
double box[SIZE]; for(int i = ;i < NUM;i ++)
if(!same(G[i].from,G[i].to))
{
unite(G[i].from,G[i].to);
box[count ++] = sqrt(G[i].cost);
if(count == N - )
break;
}
sort(box,box + count);
if(N - - M >= )
ans = box[N - - M];
else
ans = ; return ans;
}
上一篇:npm全局安装和非全局安装的区别(node v16.13.1 / npm 8.4.1)


下一篇:day12