洛谷 P2212 [USACO14MAR]浇地Watering the Fields 题解

P2212 [USACO14MAR]浇地Watering the Fields

题目描述

Due to a lack of rain, Farmer John wants to build an irrigation system to

send water between his N fields (1 <= N <= 2000).

Each field i is described by a distinct point (xi, yi) in the 2D plane,

with 0 <= xi, yi <= 1000. The cost of building a water pipe between two

fields i and j is equal to the squared Euclidean distance between them:

(xi - xj)^2 + (yi - yj)^2

FJ would like to build a minimum-cost system of pipes so that all of his

fields are linked together -- so that water in any field can follow a

sequence of pipes to reach any other field.

Unfortunately, the contractor who is helping FJ install his irrigation

system refuses to install any pipe unless its cost (squared Euclidean

length) is at least C (1 <= C <= 1,000,000).

Please help FJ compute the minimum amount he will need pay to connect all

his fields with a network of pipes.

农民约翰想建立一个灌溉系统,给他的NN (1 <= NN <= 2000)块田送水。农田在一个二维平面上,第i块农田坐标为(x_ixi​ , y_iyi​ )(0 <= x_ixi​ , y_iyi​ <= 1000),在农田ii 和农田jj 自己铺设水管的费用是这两块农田的欧几里得距离的平方(x_i - x_j)^2 + (y_i - y_j)^2(xi​−xj​)2+(yi​−yj​)2 。

农民约翰希望所有的农田之间都能通水,而且希望花费最少的钱。但是安装工人拒绝安装费用小于C的水管(1 <= CC <= 1,000,000)。

请帮助农民约翰建立一个花费最小的灌溉网络,如果无法建立请输出-1。

输入格式

  • Line 1: The integers N and C.

  • Lines 2..1+N: Line i+1 contains the integers xi and yi.

输出格式

  • Line 1: The minimum cost of a network of pipes connecting the

fields, or -1 if no such network can be built.

输入输出样例

输入 #1复制

3 11
0 2
5 0
4 3

输出 #1复制

46

说明/提示

INPUT DETAILS:

There are 3 fields, at locations (0,2), (5,0), and (4,3). The contractor

will only install pipes of cost at least 11.

OUTPUT DETAILS:

FJ cannot build a pipe between the fields at (4,3) and (5,0), since its

cost would be only 10. He therefore builds a pipe between (0,2) and (5,0)

at cost 29, and a pipe between (0,2) and (4,3) at cost 17.

Source: USACO 2014 March Contest, Silver

【思路】

生成树 + 克鲁斯卡尔 + 并查集
不得不吐槽一下
这道题作为绿题是真的有点水

先预处理出任意两个不相同的点之间的距离
用一个结构体储存起来
然后轻轻松松结构体排序一下
从第一个开始枚举
要满足先花费大于等于c
然后开始构建最小生成树
如果构建的出来
输出总花费
如果构建不出来
那就输出-1

何为构建不出来
用一个计数器计数已经使用了的边的个数
如果变数达到n-1条
也就是满足了让n个点连接的最少边数
那就可以结束了
如果枚举完全部的预处理出来的边之后
发现计数器计的数还不够n-1条边
那就是构建不出来咯

【完整代码】

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;
const int Max = 2003;
struct node
{
	int x,y;
	int w;
}a[Max * Max];
int x[Max],y[Max];
int father[Max];
int n,c;
int sum = 0;
bool cmp(const node x,const node y)
{
	return x.w < y.w;
}
int find(int x)
{
	if(father[x] != x)father[x] = find(father[x]);
	return father[x];
}
void hebing(int x,int y)
{
	x = find(x);
	y = find(y);
	father[x] = y;
}
int main()
{
	cin >> n >> c;
	for(register int i = 1;i <= n;++ i)
		father[i] = i;
	for(register int i = 1;i <= n;++ i)
		cin >> x[i] >> y[i];
	for(register int i = 1;i <= n;++ i)
	{
		for(register int j = i + 1;j <= n;++ j)
		{
			if(i != j)
			{
				a[++ sum].x = i;
				a[sum].y = j;
				a[sum].w = (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]);
			}
		}
	}
	sort(a + 1,a + 1 + sum,cmp);
	int ans = 0;
	int js = 0;
	for(register int i = 1;i <= sum;++ i)
	{
		if(a[i].w >= c)
		{
			if(find(a[i].x) != find(a[i].y))
			{
				hebing(a[i].x,a[i].y);
				js ++;
				ans += a[i].w;
			}
			if(js == n - 1)
				break;
		}
	}
	if(js != n - 1)
		cout << -1 << endl;
	else
		cout << ans << endl;
	return 0;
}
原文地址:https://www.cnblogs.com/acioi/p/11695186.html