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 ...
随机推荐
- bash shell脚本之查看系统环境变量
查看当前系统环境变量 cat test2: #!/bin/bash # display user information from the system. echo "User info f ...
- chrome不能用百度网盘极速上传插件的解决办法
进入chorme设置中,选择隐私设置中的内容设置,插件>自动运行,然后管理例外情况>添加“[*.]baidu.com”,后边当然选择允许,然后重启浏览器,OK了
- Android进阶笔记10:ListView篇之ListView显示多种类型的条目(item)
ListView可以显示多种类型的条目布局,这里写显示两种布局的情况,其他类似. 1. 这是MainActivity,MainActivity的布局就是一个ListView,太简单了这里就不写了,直接 ...
- csu 1947 三分
题意: 长者对小明施加了膜法,使得小明每天起床就像马丁的早晨一样. 今天小明早上6点40醒来后发现自己变成了一名高中生,这时马上就要做早操了,小明连忙爬起来 他看到操场密密麻麻的人,突然灵光一闪想到了 ...
- 可持久化线段树(主席树)快速简洁教程 图文并茂 保证学会。kth number例题
如果学不会也不要打我. 假设你会线段树 开始! --- 主席树也叫可持久化线段树 顾名思义,它能够保存线段树在每个时刻的版本. 什么叫每个时刻的版本?你可能对一棵普通线段树进行各种修改,这每种样子就是 ...
- Openresty最佳案例 | 第1篇:Nginx介绍
转载请标明出处: http://blog.csdn.net/forezp/article/details/78616591 本文出自方志朋的博客 Nginx 简介 Nginx是一个高性能的Web 服务 ...
- 框架篇 -- Struts2
Struts框架很很早接触的一个框架知识,已经不那么频繁使用了,但是总结一下,复习一下还是很有必要的; Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MV ...
- 解决model属性与系统重名
.h .m + (NSDictionary *)replacedKeyFromPropertyName { return @{ @"detailId" : @"id&qu ...
- iOS开发 | 自定义不规则label
其中有一个不太规则的label: image.png 这个label顶部的两个角是圆角,底部的两个角是直角,底部还有一个小三角. 思路 CAShapeLayer联合UIBezierPath画一个不 ...
- c# LRU实现的缓存类
在网上找到网友中的方法,将其修改整理后,实现了缓存量控制以及时间控制,如果开启缓存时间控制,会降低效率. 定义枚举,移除时使用 public enum RemoveType { [ ...