luogu P2872 [USACO07DEC]Building Roads S

原题链接:

       [USACO07DEC]Building Roads S - 洛谷https://www.luogu.com.cn/problem/P2872

题目描述

Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already connect some of the farms.

Each of the N (1 ≤ N ≤ 1,000) farms (conveniently numbered 1..N) is represented by a position (Xi, Yi) on the plane (0 ≤ Xi ≤ 1,000,000; 0 ≤ Yi ≤ 1,000,000). Given the preexisting M roads (1 ≤ M ≤ 1,000) as pairs of connected farms, help Farmer John determine the smallest length of additional roads he must build to connect all his farms.

给定 n 个点的坐标,第 i 个点的坐标为 (xi​,yi​),这 n 个点编号为 1 到 n。给定 m 条边,第 i 条边连接第 ui​ 个点和第 vi​ 个点。现在要求你添加一些边,并且能使得任意一点都可以连通其他所有点。求添加的边的总长度的最小值。

输入格式

* Line 1: Two space-separated integers: N and M

* Lines 2..N+1: Two space-separated integers: Xi and Yi

* Lines N+2..N+M+2: Two space-separated integers: i and j, indicating that there is already a road connecting the farm i and farm j.

第一行两个整数 n,m 代表点数与边数。
接下来 n 行每行两个整数 xi​,yi​ 代表第 i 个点的坐标。
接下来 m 行每行两个整数 ui​,vi​ 代表第 i 条边连接第 ui​ 个点和第 vi​ 个点。

输出格式

* Line 1: Smallest length of additional roads required to connect all farms, printed without rounding to two decimal places. Be sure to calculate distances as 64-bit floating point numbers.

一行一个实数代表添加的边的最小长度,要求保留两位小数,为了避免误差, 请用 6464 位实型变量进行计算。

输入输出样例

输入 #1复制

4 1
1 1
3 1
2 3
4 3
1 4

输出 #1复制

4.00

说明/提示

数据规模与约定

对于 100% 的整数,1≤n,m≤1000,1≤xi​,yi​≤10^6,1≤ui​,vi​≤n。

说明

Translated by 一只书虫仔。

Solution:

        题目大意是给你n个坐标,m条边将这n个坐标一部分连起来,然后问还要添加的边距离的最小值是多少

        题目可以比较明显的看出解法是最小生成树,但生成的联通块不一定是最小生成树,但其实我们可以将已经连好的边视为边权为0,这样就可以用最小生成树的解法了

        这里使用kruskal算法

        我们先将所有的结点两两连接的边权预处理出来,然后排序,而已经连好的边链接的点先加入并查集,接下来直接使用kruskal的模板就ok了

code:

#include<bits/stdc++.h>
using namespace std;
int f[1005];
int n,m;
struct node{
	double x,y;
}a[1005];
double sum;
struct e{
	int u,v;
	double w;
	e(){}
	e(int u1,int v1,double w1):u(u1),v(v1),w(w1){}
}edge[1000005];
int cnt;
double dis[1005][1005];
bool cmp(e a,e b)
{
	return a.w<b.w;
} 
int find(int x)
{
	return x==f[x]?x:f[x] = find(f[x]);
}
void uni(int x,int y)
{
	f[find(x)] = find(y);
}
double ddd(int x,int y)
{
	return sqrt((a[x].x-a[y].x)*(a[x].x-a[y].x)+(a[x].y-a[y].y)*(a[x].y-a[y].y));
}
int main()
{
	cin>>n>>m;
	for(int i = 1;i<=n;++i)
		f[i] = i;
	for(int i = 1;i<=n;++i)
		cin>>a[i].x>>a[i].y;
	for(int i = 1;i<n;++i)
		for(int j = i+1;j<=n;++j)
		{
			dis[i][j] = ddd(i,j);
			dis[j][i] = dis[i][j];
			edge[++cnt] = e(i,j,dis[i][j]);
		}
	sort(edge+1,edge+1+cnt,cmp);
	for(int i = 1;i<=m;++i)
	{
		int x,y;
		cin>>x>>y;
		if(find(x)!=find(y))
		{
			uni(x,y);
			n--;
		}
	}
	for(int i = 1;i<=cnt;++i)
	{
		if(n==1) break;
		if(find(edge[i].u)!=find(edge[i].v))
		{
			uni(edge[i].u,edge[i].v);
			sum+=edge[i].w;
		}
	}
	printf("%.2lf",sum);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值