题目描述

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. 组件化框架设计之Java SPI机制(三)

    阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680 本篇文章将从深入理解java SPI机制来介绍组件化框架设计: ...

  2. Spring Cloud 使用Spring Cloud Loadbalancer访问服务地址

    获取服务地址 使用的EurekaClient : getNextServerFromEureka() 使用的DiscoveryClient: getInstances() Load Balancer ...

  3. shell中#*,##*,#*,##*,% *,%% *的含义及用法

    介绍下Shell中的${}.##和%%使用范例,本文给出了不同情况下得到的结果.假设定义了一个变量为:代码如下:file=/dir1/dir2/dir3/my.file.txt可以用${ }分别替换得 ...

  4. MVC http://stackoverflow.com/tags/model-view-controller/info

    About model-view-controller Model–View–Controller (MVC) is an architectural pattern used in software ...

  5. Vue小白篇 - Vue 的指令系统 (1) v-text、v-html

    v-text:相当于innerText v-html:相当于innerHTML <div id="box"> {{ msg }} <div v-text=&quo ...

  6. WPF多线程更新UI的一个解决途径

    那么该如何解决这一问题呢?通常的做法是把耗时的函数放在线程池执行,然后切回主线程更新UI显示.前面的updateTime函数改写如下: private async void updateTime()  ...

  7. shell内置命令getopts

  8. for循环(foreach型)举例

  9. 【Java程序】约瑟夫环

    今天看视频教程无意间看到了一个数3减1的问题,百度之发现叫约瑟夫环问题,于是写了程序,问题大致描述如下: 一群带有编号的孩子手拉手围成一个圈报数,开始的孩子数1,他右边数2,再右边数3,数到n的孩子o ...

  10. eclipse中服务器找不到项目怎么解决

    在我们运行项目前,都需要将项目部署到tomcat上,但是有时我们会遇到这种情况:项目明明存在,但是eclipse中tomcat的add and remove找不到项目,无法部署,那么这个问题该如何解决 ...