[USACO14MAR]浇地Watering the Fields
题目描述
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.
输入输出样例
3 11
0 2
5 0
4 3
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的更多相关文章
- P2212 [USACO14MAR]浇地Watering the Fields
		
P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...
 - 洛谷——P2212 [USACO14MAR]浇地Watering the Fields
		
P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...
 - 洛谷 P2212 [USACO14MAR]浇地Watering the Fields 题解
		
P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...
 - 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 ...
 - 洛谷 P2212 [USACO14MAR]浇地Watering the Fields
		
传送门 题解:计算欧几里得距离,Krusal加入边权大于等于c的边,统计最后树的边权和. 代码: #include<iostream> #include<cstdio> #in ...
 - luogu题解 P2212 【浇地Watering the Fields】
		
题目链接: https://www.luogu.org/problemnew/show/P2212 思路: 一道最小生成树裸题(最近居然变得这么水了),但是因为我太蒻,搞了好久,不过借此加深了对最小生 ...
 - BZOJ3479: [Usaco2014 Mar]Watering the Fields
		
3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 81 Solved: ...
 - BZOJ 3479: [Usaco2014 Mar]Watering the Fields( MST )
		
MST...一开始没注意-1结果就WA了... ---------------------------------------------------------------------------- ...
 - bzoj 3479: [Usaco2014 Mar]Watering the Fields
		
3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 174 Solved ...
 
随机推荐
- ubuntu下MySQL的安装及远程连接配置(转)
			
1.命令窗口中输入sudo apt-get install mysql-server mysql-client 即可(配置文件位置:/etc/mysql/my.cnf 启动文件位置:/etc/init ...
 - python小学堂-基础调用
			
tem='xiaoq {0}iang'print(tem.capitalize())#首字母大学print(tem.center(30)) #居中显示iprint(tem.endswith(" ...
 - python删除某一行
			
整理了网络上的一些方法,一般有两种方法:第一种:是先把文件读入内存,在内存中修改后再写入源文件. 例子:将内容包含“123”的所有行删去: with open('C:/Users/lai/Deskto ...
 - 看FPGA面试题时见到被考到的几个逻辑电路
			
8位 D触发器: module dff8(clk , reset, d, q); input clk; input reset; :] d; :] q; :] q; always @ (posedge ...
 - wireshark 识别http的标准
			
使用route add 本机ip 255.255.255.255 网关ip metric 1 的方式 会使访问本机ip的连接发送到局域网内,这样wireshark就可以抓取到 但有一点需要注意 如果本 ...
 - C#简单的文件依赖缓存的使用
			
一,FileCache.aspx页面 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind=& ...
 - NULL合并操作符??
			
参考官方手册: /** * NULL合并操作符 ?? */ // $a, $b, $c都未声明和定义 var_dump($a??$b??$c); // NULL // $a为数组,$b为100,$c为 ...
 - 2019-1-28-WPF-高性能笔
			
title author date CreateTime categories WPF 高性能笔 lindexi 2019-1-28 14:21:5 +0800 2018-2-13 17:23:3 + ...
 - How can I check the last time stats was run on Oracle without using OEM
			
All of the following data dictionary tables have a LAST_ANALYZED column (replace * with USER/ALL/DBA ...
 - Delphi DBGrid 实现复选框
			
1 在数据库对应的表中加入 bit 列验证是否被选中 然后dbgrid第一列的filedname是bit列 在DBgrid的onDrawColumnCell事件中写: procedure DBGri ...