[UVA][MST] 10369 - Arctic Network
Problem C: Arctic Network
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.
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.
Sample Input
1 2 4 0 100 0 300 0 600 150 750
Sample Output
212.13
求最小生成樹的最大邊, 但又衛星可以直接協助連接, 因此可以少算 S 個邊
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct E {
int x, y;
int v;
};
E D[250000];
int cmp(const void *i, const void *j) {
E *a, *b;
a = (E *)i, b = (E *)j;
return a->v - b->v;
}
int p[501], r[501];
void init(int n) {
int i;
for(i = 0; i <= n; i++)
p[i] = i, r[i] = 1;
}
int find(int x) {
return p[x] == x ? x : (p[x] = find(p[x]));
}
int joint(int x, int y) {
x = find(x), y = find(y);
if(x != y) {
if(r[x] > r[y])
p[y] = x, r[x] += r[y];
else
p[x] = y, r[y] += r[x];
return 1;
}
return 0;
}
int main() {
int t, S, P;
int x[501], y[501], i, j;
scanf("%d", &t);
while(t--) {
scanf("%d %d", &S, &P);
int m = 0;
for(i = 0; i < P; i++) {
scanf("%d %d", &x[i], &y[i]);
for(j = i-1; j >= 0; j--) {
D[m].x = i, D[m].y = j;
D[m].v = (x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]);
m++;
}
}
qsort(D, m, sizeof(E), cmp);
init(P);
int cnt = 0;
for(i = 0; i < m; i++) {
cnt += joint(D[i].x, D[i].y);
if(cnt == P-S) {
printf("%.2lf\n", sqrt(D[i].v));
break;
}
}
}
return 0;
}
上一篇:[UVA] 412 - Pi