uva 10369 Arctic Network 最小生成树上的第k条边

开始的时候想错了,以为必须是邻接点才能使用卫星信号所以开始错了好几次!后来看了网上的说法才知道不是直接求出s-1大的边就可以了!(其实还是不明白题意)


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <cstring>

#define INF 100000000
using namespace std;
int x[1005];
int y[1005];
int vis[1005];
int fa[1005];


struct node{
	int x,y,w;
	bool operator < (const node &a)const{
		return w < a.w;
		
	}
	bool operator > (const node &a)const{
		return w > a.w;
	}
};

int fun(int x){
	return fa[x] ==x ? x : fun(fa[x]);
	
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int s,p;
		
		cin >> s >> p;
		
		for(int i = 0;i < p;i++){
			cin >> x[i] >> y[i];
		}
		priority_queue<node,vector<node>,greater<node> > que;
		
		for(int i = 0;i < p;i++){
			for(int j = 0;j < p;j++){
				if(i != j){
					node a;
					a.x = i;
					a.y = j;
					a.w = (x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
					que.push(a);		
				}
			}
		}
		
		for(int i = 0;i < p;i++){
			fa[i] = i;
		}
		priority_queue<node,vector<node>,less<node> > ant;
		
		while(!que.empty()){
			node a = que.top();
			que.pop();
			
			int fx = fun(a.x);
			int fy = fun(a.y);
			if(fx != fy){
				fa[fx] =fy;
				ant.push(a);
			}
		}
		
		memset(vis,0,sizeof(vis));
		 	
		
		while(s > 1){
			node a = ant.top();
			s --;
			ant.pop();	
		}
		node a = ant.top();
		printf("%.2f\n",sqrt(a.w));
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值