hdu1875 畅通工程再续

题目链接
此题是求最小生成树的变形,需要从点和坐标中抽象出边的信息,再利用kruskal求得最小生成树,不过要注意边是否符合要求,不然无法使用。看最后能否构成最小生成树;

**#include <iostream>
#include<stdio.h>
#include<cmath>
#include<algorithm>
using namespace std;
struct edge
{
	int a, b;
	double cost;
	bool operator <(const edge& a)const {
		return cost < a.cost;
	}
}buf[5000];
struct point {
	int x;
	int y;
	double getdistance(point a) {
		double ans = (x - a.x) * (x - a.x) + (y - a.y) * (y - a.y);
		return sqrt(ans);
	}
}list[105];
int tree[105];
int find(int x)//并查集找根
{
	int ret;
	int tmp = x;
	while (tree[x] != -1) x = tree[x];
	ret = x;
	x = tmp;
	while (tree[x] != -1)
	{
		int t = tree[x];
		tree[x] = ret;
		x = t;
	}
	return ret;
}
int main()
{
	int c;
	cin >> c;
	while (c--)
	{
		int n;
		cin >> n;
		for (int i = 1;i <= n;i++) cin >> list[i].x >> list[i].y;
		int size = 0;
		for(int i=1;i<=n;i++)
			for (int j = i + 1;j <= n;j++)
			{
				if (list[i].getdistance(list[j]) >= 10 && list[i].getdistance(list[j]) <= 1000)//只取符合要求的边,在这一步提前判断边。
				{
					buf[size].a = i;
					buf[size].b = j;
					buf[size].cost = list[i].getdistance(list[j]);
					size++;
				}

			}
		sort(buf, buf + size);
		for (int i = 1;i <= n;i++) tree[i] = -1;//对数组初始化,每一个都是一个根
		double ans = 0;
		for (int i = 0;i < size;i++)
		{
			int a = find(buf[i].a);
			int b= find(buf[i].b);
			if (a != b) {
				ans += buf[i].cost;
				tree[a] = b;
			}
		}
		int count = 0;
		for (int i = 1;i <= n;i++)
		{
			if (tree[i] == -1) count++;//通过判断连通分量的个数来确定是否能构成了一个最小生成树。
		}
		if (count == 1) printf("%.1lf\n", ans * 100);
		else cout << "oh!" << endl;
	}
	return 0;
}**
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值