UVA - 10369 Arctic Network (最小生成树)

题目大意:
有n个科研站, 要把这些站用卫星或者无线电连接起来,使得任意两个都能直接或者间接相连。任意两个都有安装卫星设备的,都可以直接通过卫星通信,不管它们距离有多远。而安装有无线电设备的两个站,距离为D。D和费成正比。
现在要用s个卫星设备把所有城市都连接起来,求最长边的小代价。其中某些城市可以直接用卫星连接、没有长度。求一个方案,使得费用D最少(D取决与所有用无线电通信的花费最大的那条路径)。

解析:s个通信卫星可以安装s-1条最长的那些路径。那么, 最小生成树中第p-s大的路径长度就是D。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 1000;
struct Point {
	double x,y;
}p[N];
struct Edge {
	int s,e;
	double v;
}a[N*N/2];
int n,m;
int pa[N];
double save[N];

bool cmp(Edge a,Edge b) {
	return a.v < b.v;
}
double dis(Point a, Point b) {
	return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
int find(int x) {
	if(x == pa[x]) {
		return x;
	}else {
		return find(pa[x]);
	}
}
void init() {
	for(int i = 0; i <= n; i++) {
		pa[i] = i;
	}
}
int main() {
	int t;
	scanf("%d",&t);
	while(t--) {
		scanf("%d%d",&m,&n);
		for(int i = 1; i <= n; i++) {
			scanf("%lf%lf",&p[i].x,&p[i].y);
		}
		int tot = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = i+1; j <= n; j++) {
				a[tot].s = i;
				a[tot].e = j;
				a[tot].v = dis(p[i],p[j]);
				tot++;
			}
		}
		init();
		sort(a,a+tot,cmp);
		int num = 0;
		for(int i = 0; i < tot; i++) {
			int k = find(a[i].s) ,g = find(a[i].e);
			if(k != g) {
				pa[g] = k;
				num++;
			}
			if(num == n - m) {
				printf("%.2lf\n",a[i].v);
				break;
			}
		}
	}
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值