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. 如何给VirtualBox虚拟机的ubuntu LVM分区扩容

    我在VirtualBox安装的ubuntu里安装Cloud Foundry时遇到错误信息,磁盘空间不够了: 使用这三个命令做了清理之后,结果依然不够理想: (1) sudo apt-get autoc ...

  2. python+requests+json 接口测试思路示例

    实际项目中用python脚本实现接口测试的步骤: 1 发送请求,获取响应  >>2 提取响应里的数据,对数据进行必要的处理  >>3 断言响应数据是否与预期一致 以豆瓣接口为例 ...

  3. IOS UIDevice距离传感器(打开 关闭)

    ●  什么是传感器 ●  传感器是一种感应\检测装置, 目前已经广泛应用于智能手机上 ●  iPhone5中内置的传感器有 ●  运动传感器\加速度传感器\加速计(Motion/Acceleromet ...

  4. C/C++ memmove与memcpy的区别及实现

    1.与字符串函数strcpy区别: memcpy与memmove都是对内存进行拷贝可以拷贝任何内容,而strcpy仅是对字符串进行操作. memcpy与memmove拷贝多少是通过其第三个参数进行控制 ...

  5. 【LOJ6062】「2017 山东一轮集训 Day2」Pair(线段树套路题)

    点此看题面 大致题意: 给出一个长度为\(n\)的数列\(a\)和一个长度为\(m\)的数列\(b\),求\(a\)有多少个长度为\(m\)的子串与\(b\)匹配.数列匹配指存在一种方案使两个数列中的 ...

  6. POJ 1011 Sticks 【DFS 剪枝】

    题目链接:http://poj.org/problem?id=1011 Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissio ...

  7. PAML学习一

    前言 模式识别起源于工程,而机器学习从计算机科学中产生.然而这两者被看做同一领域的两方面,过去十年里他们获得了极大的发展.特别是,贝叶斯方法已经发展成主流,而图模型已经被融入用于描述和应用概率模型的通 ...

  8. c# 常见验证邮箱、电话号码、日期等格式

    #region 验证邮箱验证邮箱 /**//// <summary> /// 验证邮箱 /// </summary> /// <param name="sour ...

  9. JavaScript中的Map和Set

    JavaScript的默认对象表示方法{}可以视为其他语言中的Map或者Dictionary的数据结构,即一组键值对. 但是JavaScript的对象有个小问题,就是键必须是字符串,但实际上Numbe ...

  10. 导航栏的ul中的li设置问题

    在css中 设置li的float:left 可以实现列表在同一行显示 设置每个li的宽度相等,可以实现每个列表的分离状态. 设置每个li中的文字,text-align:center; 可实现每个列表的 ...