题目描述

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. 用MR生成HFile文件格式后,数据批量导入HBase

    环境hadoop cdh5.4.7 hbase1.0.0 测试数据: topsid  uid roler_num typ 10 111111 255 0 在Hbase 创建t2数据库: create ...

  2. Jquery中input:type=radio的监听,获取设置值

    一.html <div id='demo'> <input type='radio' name='sex' value='男' > <input type='radio' ...

  3. php Connection timed out after 30000 milliseconds

    function HttpRequest($url, $params, $method = 'GET', $header = array(), $bEncode = true){ $opts = ar ...

  4. CSS学习笔记(基础部分)

    一.CSS 简介: CSS 指层叠样式表 (Cascading Style Sheets),用来设置HTML的格式. 语法:选择器 {属性:值;属性:值} 注释://注释内容 /*注释内容*/ 二.C ...

  5. elasticsearch 中的Multi Match Query

    在Elasticsearch全文检索中,我们用的比较多的就是Multi Match Query,其支持对多个字段进行匹配.Elasticsearch支持5种类型的Multi Match,我们一起来深入 ...

  6. 分支结构if 语句语法

  7. 4python 解析库的使用

    4.1 xml库 https://cuiqingcai.com/5545.html XPath,全称XML Path Language,即XML路径语言,它是一门在XML文档中查找信息的语言.它最初是 ...

  8. vue-router中的router-link的active-class

    vue-router中的router-link的active-class   在vue-router中要使用选中样式的方法有两种: 1.直接在路由js文件中配置linkActiveClass 2.在r ...

  9. translation of 《deep learning》 Chapter 1 Introduction

    原文: http://www.deeplearningbook.org/contents/intro.html Inventors have long dreamed of creating mach ...

  10. Debug和Release区别(转)

    地址:https://zhidao.baidu.com/question/629188090208609884.html 最近写代码过程中,发现 Debug 下运行正常,Release 下就会出现问题 ...