POJ - 2349UVA - 10369 Arctic Network(最小生成树求权值第k大的边)(内附两种算法)

家资是何物,积帙列梁梠。这篇文章主要讲述POJ - 2349UVA - 10369 Arctic Network(最小生成树求权值第k大的边)(内附两种算法)相关的知识,希望能为你提供帮助。
【POJ - 2349UVA - 10369 Arctic Network(最小生成树求权值第k大的边)(内附两种算法)】题干:
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

题目大意:
南极有n个科研站, 要把这些站用卫星或者无线电连接起来,使得任意两个都能直接或者间接相连。任意两个都有安装卫星设备的,都可以直接通过卫星通信,不管它们距离有多远。 而安装有无线电设备的两个站,距离不能超过D。 D越长费用越多。
现在有s个卫星设备可以安装,还有足够多的无线电设备,求一个方案,使得费用D最少(D取决与所有用无线电通信的花费最大的那条路径)。
题意:有n个点给出坐标,点和点之间可以用无线电或者卫星通信,每个点都有无线电收发器可进行无线电通信,但是只有m个点有卫星通信功能。卫星通信的距离可以无限大,但无线电通信的距离不能超过D,超过D的部分将使通信费用增加。要使通信费用最少。
其实就是求一次最小生成树,m个点有卫星通信,那么就会有m-1条边的通信距离无限大,其实就是这m-1条边不用计算费用。而剩下的边中,找出最大边作为D值,这样剩下的所有的边都不会大于D,那么不会增加通信费用。在构建MST过程中保存下所有的边权值,然后按升序排序,除掉最后的m-1条边(也就是最大的m-1条边,这些边用卫星通信),最大的那条就是D;
解题报告:
      注意题目中   有s个卫星,也就是说可以连s-1条边!所以最后答案还要+1
ac代码:(329ms   2.2MB)(用m==n-1跳出循环也才313ms)(用double然后算dist的时候就用sqrt也不算慢,,344ms)
#include< iostream>
#include< cmath>
#include< cstdio>
#include< algorithm>
const int MAX = 500 + 5;

using namespace std;
int s,p;
int top;
long long ans[1000];
int f[MAX];
struct Node
int x,y;
node[MAX];
struct Edge
int u,v; //别写x,y、其实更准确说应该是u和v,因为这时候你存的是两个点。你如果用x和y代表一个点的话,那就需要用x1x2y1y2来表示Edge结构体了
long long w; //权值 在此处就是距离
//Edge(int u,int v,long long w) : u(u),v(v),w(w)
edge[MAX * MAX + 5];
bool cmp(const Edge & a,const Edge & b)
return a.w< b.w;


int getf(int v)
return f[v] == v ? v : f[v] = getf(f[v] );

void merge(int u, int v)
int t1 = getf(u );
int t2 = getf(v );
if(t1 != t2)
f[t2] =t1;


int main()

int t;
cin> > t;
long long dist;
while(t--)
//留给初始化
top = 0;
for(int i = 0; i< MAX; i++)
f[i]=i;

scanf("%d %d",& s,& p); //有 p 个点, 即p就是以前的n
for(int i= 0; i< p; i++) //从0开始读的!
scanf("%d %d",& node[i].x,& node[i].y);

for(int i = 0; i< p; i++)
for(int j = i+1; j< p; j++)
top++;
dist = (node[i].x-node[j].x )*(node[i].x-node[j].x ) + (node[i].y-node[j].y )*(node[i].y-node[j].y );
//edge[top] = (i,j,dist);
//printf("****%lld\\n",dist);
edge[top].u=i; edge[top].v=j; edge[top].w=dist;


int cur=0;
sort(edge + 1,edge+top +1,cmp);
for(int i = 1; i< =top; i++)
if(getf(edge[i].u) != getf(edge[i].v ) )
merge(edge[i].u ,edge[i].v );
++cur;
ans[cur] = edge[i].w;



//printf("cur = %d\\n",cur);
//for(int i = 1; i< =cur; i++)
//printf("%lld",ans[i]);
//
printf("%.2f\\n",sqrt(ans[cur-(s - 1) ] * 1.0) );

return 0 ;

AC代码2:(prim算法)(32ms,2.1MB)
#include< cstdio>
#include< cstring>
#include< cmath>
#include< algorithm>
#define N 505
using namespace std;
double x[N], y[N], w[N][N], key[N], ans[N*N];
int n,m,pre[

    推荐阅读