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. 浏览器下出现net::ERR_BLOCKED_BY_CLIENT的解决办法

    转发网址:https://www.cnblogs.com/wenzheshen/p/7724065.html 当我们在做开发时,调试页面图片会出现部分图片无法正常显示,并且确认图片的地址正确: 按F1 ...

  2. 2018.10.2 Eclipse中如何测地修改一个we项目步骤

    找到项目的web.xml文件 大概的路径如下: 修改xml文件中的display-name 节点的值 下一步就是切换工作目录 显示的效果 打开最后一个文件修改 接下来找到这个文件 是部署的时候用的 运 ...

  3. 安卓extends和implements

    extends是继承类 implements是实现接口

  4. js前端解析excel文件

    使用纯Javascript解析excel文件. 使用的是开源的解析excel的js库:sheetjs.github地址:https://github.com/SheetJS/js-xlsx 首先引用J ...

  5. Storm 出现 no jzmq in java.library.path

    在真实环境中运行时,在log日志下,查看workpid日志发现出现该错误. 解决办法: 在conf/storm.yaml添加jzmq安装的路径, 我使用的默认安装在/usr/local/lib下 ja ...

  6. 理解Storm可靠性消息

    看过一些别人写的, 感觉有些东西没太说清楚,个人主要以源代码跟踪,参考个人理解讲述,有错误请指正. 1基本名词 1.1 Tuple: 消息传递的基本单位.很多文章中介绍都是这么说的, 个人觉得应该更详 ...

  7. shell命令查看某文件夹下的文件个数

    shell命令查看某文件夹下的文件个数 2010-06-25 17:05:15|  分类: shell |字号 订阅   1.查看某文件夹下文件的个数: ls -l |grep "^-&qu ...

  8. OS_EVENT 信号量

    1.   OS_EVENT *T2sem=(OS_EVENT *)0; 这句代码的意思是 把OS_EVENT类型的一个指针T2sem赋值为0: 其中 OS_EVENT是数据类型,*代表是指针类型,(O ...

  9. block与inline,inline和inline-block,块级和行内元素,行内替换和行内非替换元素

    block:块级元素默认display属性为block:无论块内内容有多少,总是占满一行: inline:行内元素默认display属性为inline:只占据块内的内容的大小,不会占满一整行: inl ...

  10. box-shadow的应用技巧

    一.box-shadow的参数解析 box-shadow:none; box-shadow: h-shadow v-shadow blur spread color inset; box-shadow ...