题目描述

Due to a lack of rain, Farmer John wants to build an irrigation system tosend 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 twofields 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 hisfields are linked together -- so that water in any field can follow asequence of pipes to reach any other field.

Unfortunately, the contractor who is helping FJ install his irrigationsystem refuses to install any pipe unless its cost (squared Euclideanlength) 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.

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

农民约翰希望所有的农田之间都能通水,而且希望花费最少的钱。但是安装工人拒绝安装费用小于C的水管(1 <= C<= 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的关系,进而决定是否加边,但是注意数组是2000*2000,而不是2000,否则会RE。

CODE:

 #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int M=;
int n,c,tot,ans;
int fa[M];
int xcor[M],ycor[M];
struct node{
int u,v,w;
}a[M];
int findr(int x){
if (fa[x]==x) return x;
return fa[x]=findr(fa[x]);
}
void merge(int x,int y){
int A=findr(x);
int B=findr(y);
if (fa[A]!=fa[B]) fa[B]=A;
return ;
}
bool cmp(node x,node y){return x.w<y.w;}
int main(){
cin>>n>>c;
for (int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=n;i++) cin>>xcor[i]>>ycor[i];
for (int i=;i<=n;i++){
for (int j=i+;j<=n;j++){
int cost=(xcor[i]-xcor[j])*(xcor[i]-xcor[j])+(ycor[i]-ycor[j])*(ycor[i]-ycor[j]);
if (cost>=c){
a[++tot].u=i;
a[tot].v=j;
a[tot].w=cost;
}
}
}
sort(a+,a+tot+,cmp);
int cnt=;
for (int i=;i<=tot;i++){
if (fa[findr(a[i].u)]!=fa[findr(a[i].v)]){
ans+=a[i].w;
merge(a[i].u,a[i].v);
cnt++;
}
}
if (cnt==n-) cout<<ans<<endl;
else cout<<-<<endl;
return ;
}

[USACO14MAR]浇地Watering the Fields的更多相关文章

  1. P2212 [USACO14MAR]浇地Watering the Fields

    P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...

  2. 洛谷——P2212 [USACO14MAR]浇地Watering the Fields

    P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...

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

    P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...

  4. P2212 [USACO14MAR]浇地Watering the Fields 洛谷

    https://www.luogu.org/problem/show?pid=2212 题目描述 Due to a lack of rain, Farmer John wants to build a ...

  5. 洛谷 P2212 [USACO14MAR]浇地Watering the Fields

    传送门 题解:计算欧几里得距离,Krusal加入边权大于等于c的边,统计最后树的边权和. 代码: #include<iostream> #include<cstdio> #in ...

  6. luogu题解 P2212 【浇地Watering the Fields】

    题目链接: https://www.luogu.org/problemnew/show/P2212 思路: 一道最小生成树裸题(最近居然变得这么水了),但是因为我太蒻,搞了好久,不过借此加深了对最小生 ...

  7. BZOJ3479: [Usaco2014 Mar]Watering the Fields

    3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 81  Solved: ...

  8. BZOJ 3479: [Usaco2014 Mar]Watering the Fields( MST )

    MST...一开始没注意-1结果就WA了... ---------------------------------------------------------------------------- ...

  9. bzoj 3479: [Usaco2014 Mar]Watering the Fields

    3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 174  Solved ...

随机推荐

  1. ID和Phone高压缩比存储和查询

    ID和Phone高压缩比存储和查询的简单例子, 无多线程处理 运行环境JDK8+maven 0. 模块分割 1. 基本思路 源文件BCP每一行都转为一个全局的RowID,可以直接映射到FileName ...

  2. ES6/ES2015核心内容 import export

    ES6/ES2015核心内容:https://www.cnblogs.com/doit8791/p/5184238.html Javascript ES6学习 import export  https ...

  3. java并发编程之美-阅读记录7

    java并发包中的并发队列 7.1ConcurrentLinkedQueue 线程安全的无界非阻塞队列(非阻塞队列使用CAS非阻塞算法实现),其底层数组使用单向列表实现,对于出队和入队操作使用CAS非 ...

  4. IntelliJ IDEA 常用快捷键和技巧

    IntelliJ Idea 常用快捷键列表 Alt+回车 导入包,自动修正Ctrl+N  查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L  格式化代码Ctrl+Alt+O 优化导入的类和 ...

  5. go语言从例子开始之Example6.if/else

    if 和 else 分支结构在 Go 中当然是直接了当的了. package main import "fmt" func main() { 这里是一个基本的例子. if 7%2 ...

  6. IPv6 关于路由器配置静态IPv6路由的命令

    今天在学习路由器配置ipv6 的时候遇到了一点疑惑 一条命令为:ipv6 route FE80:0202::/32 serail 0/1 201 一条命令为:ipv6 route FE80:0202: ...

  7. jQuery 菜单 水平菜单的实现

    html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...

  8. oracle 数据库 锁

    首先你要知道表锁住了是不是正常锁?因为任何DML语句都会对表加锁. 你要先查一下是那个会话那个sql锁住了表,有可能这是正常业务需求,不建议随便KILL session,如果这个锁表是正常业务你把se ...

  9. jQuery判断checkbox是否选中的4种方法

    方法一: ).checked) { // do something } 方法二: if($('#checkbox-id').is(':checked')) { // do something } 方法 ...

  10. 【leetcode】909. Snakes and Ladders

    题目如下: 解题思路:天坑题,不在于题目多难,而是要理解题意.题目中有两点要特别注意,一是“You choose a destination square S with number x+1, x+2 ...