P2212 [USACO14MAR]浇地Watering the Fields
P2212 [USACO14MAR]浇地Watering the Fields
题目描述
Due to a lack of rain, Farmer John wants to build an irrigation system to
send 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 two
fields 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 his
fields are linked together -- so that water in any field can follow a
sequence of pipes to reach any other field.
Unfortunately, the contractor who is helping FJ install his irrigation
system refuses to install any pipe unless its cost (squared Euclidean
length) 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块农田坐标为(xi, yi)(0 <= xi, yi <= 1000),在农田i和农田j自己铺设水管的费用是这两块农田的欧几里得距离(xi - xj)^2 + (yi - yj)^2。
农民约翰希望所有的农田之间都能通水,而且希望花费最少的钱。但是安装工人拒绝安装费用小于C的水管(1 <= C <= 1,000,000)。
请帮助农民约翰建立一个花费最小的灌溉网络。
输入输出格式
输入格式:
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
分析:最小生成树,kruskal算法,首先建图,按权值排序,kruskal时注意小于c的边不能使用,直接continue,最后注意如果计数器p不等于N-1,输出-1.
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN = ;
struct Edge{
int x,y,w;
bool operator < (const Edge &a) const
{
return w < a.w;
}
}e[MAXN*MAXN];
struct node{
int x,y;
}t[MAXN];
int far[MAXN];
int n,c,cnt,ans,p; int find(int a)
{
return a==far[a]?a:far[a]=find(far[a]);
}
int main()
{
scanf("%d%d",&n,&c);
for (int i=; i<=n; ++i)
{
scanf("%d%d",&t[i].x,&t[i].y);
far[i] = i;
}
for (int i=; i<=n; ++i)
for (int j=i+; j<=n; ++j)
{
e[++cnt].x = i;
e[cnt].y = j;
e[cnt].w = abs(t[i].x-t[j].x)*abs(t[i].x-t[j].x)+abs(t[i].y-t[j].y)*abs(t[i].y-t[j].y);
}
sort(e+,e+cnt+);
for (int i=; i<=cnt; ++i)
{
if (e[i].w<c) continue ;
int rx = find(e[i].x);
int ry = find(e[i].y);
if (rx!=ry)
{
++p;
far[rx] = ry;
ans += e[i].w;
if (p==n-) break ;
}
}
if (p==n-) printf("%d",ans);
else printf("-1");
return ;
}
P2212 [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 洛谷
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 思路: 一道最小生成树裸题(最近居然变得这么水了),但是因为我太蒻,搞了好久,不过借此加深了对最小生 ...
- [USACO14MAR]浇地Watering the Fields
题目描述 Due to a lack of rain, Farmer John wants to build an irrigation system tosend water between his ...
- 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 ...
随机推荐
- Velocity 模板
Velocity 模板引擎介绍 引:https://www.ibm.com/developerworks/cn/java/j-lo-velocity1/ 在 现今的软件开发过程中,软件开发人员将更多的 ...
- fast、faster中ap值的计算
def voc_ap(rec, prec, use_07_metric=False): """ ap = voc_ap(rec, prec, [use_07_metric ...
- TridentState分析
public class TridentState { TridentTopology _topology; Node _node; protected TridentState(TridentTop ...
- Rest API 开发 学习笔记
概述 REST 从资源的角度来观察整个网络,分布在各处的资源由URI确定,而客户端的应用通过URI来获取资源的表示方式.获得这些表徵致使这些应用程序转变了其状态.随着不断获取资源的表示方式,客户端应用 ...
- SpringBoot非官方教程 | 第三篇:SpringBoot用JdbcTemplates访问Mysql
转载请标明出处: 原文首发于https://www.fangzhipeng.com/springboot/2017/07/11/springboot3-JdbcTemplates-Mysql/ 本文出 ...
- oracle快速添加用户及授权
--Oracle使用的是用户管理模式--意味着,Oracle的数据使用用户来分割 --以后开发,我们需要每个项目都需要使用一个用户 --所以:一个数据文件是可以放多个用户的数据的.但是我们开发从数据的 ...
- CentOS 7设置网卡开机自动启用
一.查看网卡配置 root权限 [root@dbsyn ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue ...
- 获取APP地图权限
获取APP地图权限 NSLocationWhenUseUsageDescription,在info里面设置为空
- 你不知道的javaScript笔记(5)
原生函数 常用的原生函数 String() Number() Boolean() Array() Object() Function() RegExp() Date() Error() Symbol( ...
- WIN10 vagrant和virtualbox虚拟机和一键搭建lnmp环境配置thinkphp虚拟主机
版本:win10系统 virtualbox:5.1.26 vagrant :1.9.7 centos 7.0 xshell/git 首先下载好对应版本的软件 配置vagrant和virtualbox ...