Arctic Network
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 Source
Waterloo local 2002.09.28
|
Description
Input
Output
Sample Input
1 2 4 0 100 0 300 0 600 150 750
Sample Output
212.13
这道题我现在的水平也不能给出一个合理的证明过程,但是计算的方法现在懂了,就是先按照克鲁斯塔尔的方法生成最小树,并用一个数组保存生成树时边的大小,s个卫星可以代替s-1条边,这样求次s-1大的边就行了。
代码如下:
#include <cstdio> #include <algorithm> #include <cstring> #include <cmath> using namespace std; int f[511]; struct node1 { int st,end; double cost; }road[500*250]; struct node2 { double x,y; }data[511]; int find(int x) { if (x!=f[x]) f[x]=find(f[x]); return f[x]; } int join (int x,int y) { int fx,fy; fx=find (x); fy=find (y); if (fx!=fy) { f[fx]=fy; return 1; } return 0; } bool cmp1(node1 a,node1 b) { return a.cost<b.cost; } bool cmp2 (int a,int b) { return a<b; } double dis(node2 a,node2 b) { double ans; ans=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); return ans; } int main() { int n,s; double d[511]; //记录生成树的边数 int u; scanf ("%d",&u); while (u--) { scanf ("%d %d",&s,&n); for (int i=1;i<=n;i++) f[i]=i; //别忘了初始化 for (int i=1;i<=n;i++) scanf ("%lf %lf",&data[i].x,&data[i].y); int num=-1; for (int i=1;i<n;i++) { for (int j=i+1;j<=n;j++) { num++; road[num].st=i; road[num].end=j; road[num].cost=dis(data[i],data[j]); } } sort (road,road+num+1,cmp1); //这里注意num要 +1 int t=-1; for (int i=0;i<=num;i++) { if (join(road[i].st,road[i].end)) { t++; d[t]=road[i].cost; } } sort (d,d+t+1,cmp2); printf ("%.2lf\n",d[n-1-s]); } return 0; }