码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-05-17 09:24:21      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:最小生成树   kruskal   

开始的时候想错了,以为必须是邻接点才能使用卫星信号所以开始错了好几次!后来看了网上的说法才知道不是直接求出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;
}


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

标签:最小生成树   kruskal   

原文地址:http://blog.csdn.net/qq_24667639/article/details/45786145

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!