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.

输入输出样例

输入样例#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

分析:最小生成树,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的更多相关文章

  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 洛谷

    https://www.luogu.org/problem/show?pid=2212 题目描述 Due to a lack of rain, Farmer John wants to build a ...

  4. 洛谷 P2212 [USACO14MAR]浇地Watering the Fields

    传送门 题解:计算欧几里得距离,Krusal加入边权大于等于c的边,统计最后树的边权和. 代码: #include<iostream> #include<cstdio> #in ...

  5. luogu题解 P2212 【浇地Watering the Fields】

    题目链接: https://www.luogu.org/problemnew/show/P2212 思路: 一道最小生成树裸题(最近居然变得这么水了),但是因为我太蒻,搞了好久,不过借此加深了对最小生 ...

  6. [USACO14MAR]浇地Watering the Fields

    题目描述 Due to a lack of rain, Farmer John wants to build an irrigation system tosend water between his ...

  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. typescript import需要注意的地方以及一点疑问

    在使用 import {XXX} from './xxx'的时候,到浏览器上会报错,提示找不到xxx文件,原因在于没有加入后缀,这时候要写成import {XXX} from './xxx.js'注意 ...

  2. bootstrap table 怎么实现前两列固定冻结?

    $("#Table").bootstrapTable('destroy').bootstrapTable({ pagination: true,//分页 minimumCountC ...

  3. ACM-ICPC(10 / 9)

    ACM-ICPC(10.9) 树形DP 树形DP考点很多,状态转移有时会很复杂,但是也有规律可寻,最重要的是抓住父子关系之间的状态转移. 树的最大独立集:尽量选择多的点,使得任何两个结点均不相邻.​ ...

  4. 【[COCI2011-2012#5] POPLOCAVANJE】

    据说这道题卡空间? 不存在的,拿\(AC\)自动机去存\(5000\times5000\)的串肯定是要M的 我们可以考虑对长度为\(n\)的串建一个\(SAM\),这样空间就只需要两倍的\(3e5\) ...

  5. GetClassLoader和GetCallerClass的使用

    GetClassLoader是JAVA中用来得到ClassLoader的 JAVA中有以下几种ClassLoader. 1.  Bootstrap ClassLoader - GetClassLoad ...

  6. 记安卓appium自动化测试实践

    一.软件安装 1. 安装node.js,安装路径D:\Program Files\nodejs\ 可以在官网下载https://nodejs.org/zh-cn/download/,版本号为node- ...

  7. sed实现路径替换

    shell和sed忘得差不多了,现在更喜欢用python notebook,可以自动补充,所以很方便.但是记得以前用过这个的,试了几次不成功,搜了一下,这几个地方给的比较清晰,尤其是路径替换. 以下内 ...

  8. o'Reill的SVG精髓(第二版)学习笔记——第六章

    第六章:坐标系统变换 想要旋转.缩放或者移动图片到新的位置.可以给对应的SVG元素添加transform属性. 6.1 translate变换 可以为<use>元素使用x和y属性,以在特性 ...

  9. ATK-DataPortal 设计框架(二)

    在信息的交换过程中,总是有此相同相似的功能,由于业务的各自不同,由同一类型来处理诸如增删改查等常见的信息处理方式.从日常的对些类行为操作为生成的类分析,大量雷同的代码遍布整个项目.框架中xxxHand ...

  10. Plupload+easyui+springmvc实现批量上传

    demo下载(java项目):http://pan.baidu.com/s/1ntmoGEd 可兼容所有常用浏览器,当前版本为V1.5.4,如果不兼容,肯定是你没有调试好啊 1.jsp代码 <% ...