题目描述

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. 解惑结构体与结构体指针(struct与typedef struct在数据结构的第一道坎)

    /* 数据结构解惑01  在数据结构中会看到 typedef struct QNode { QElemType data; //数据域 struct QNode *next; //指针域 }QNode ...

  2. mac 支持rz sz

    安装 lrzsz brew install lrzsz 配置 iTerm2 安装完成后我们需要在 iTerm2 中使用的话,还需要一些配置 进入到 /usr/local/bin 目录下,下载两个脚本文 ...

  3. python字符串方法学习笔记

    # 一.字符串大小写转换# 字符串首字符大写print("hello world".capitalize())# 将字符串变为标题print("hello WORLD&q ...

  4. String类可以被继承吗?我们来聊聊final关键字!

    String类可以被继承吗?我们来聊聊final关键字! String在java基础知识中绝对是个重点知识,关于String的一些问题也是非常的多,而且牵涉到内存等高级知识,在面试中也是经常被考察的一 ...

  5. c# 编程--基础部分补全篇

    C#基础 分支: switch            switch(表达式)                {                    case 具体值1:                ...

  6. Robot Framework使用技巧之内部变量

    [转载] 1.变量的使用 变量可以在命令行中设置,个别变量设置使用--variable (-v)选项,变量文件的选择使用--variablefile (-V)选项. 通过命令行设置的变量是全局变量,对 ...

  7. C语言结构体实例-创建兔子

    参考裸编程思想. #include <stdio.h> //#include "ycjobject.h" // 颜色定义 #define CL_BLACK 0 #def ...

  8. js手机端图片弹出方法

    1 $("img").click(function(){ //获取窗口可视大小 var width=$(window).width(); var height=$(window). ...

  9. Python内置数学函数

    class NumString: def __init__(self, value): self.value = str(value) def __str__(self): return self.v ...

  10. Flink(一)集群配置

    三台主机 centos6 已经完成的工作: 防火墙已关闭 主机名修改完毕,ssh免密登陆配置完成 jdk已安装 zookeeper已经部署并运行 hadoop已经部署并运行 版本:flink-1.8. ...