Watering the Fields

时间限制: 1 Sec  内存限制: 64 MB
提交: 26  解决: 10
[提交][状态][讨论版]

题目描述

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.

输入

* 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

提示

There are 3 fields, at locations (0,2), (5,0), and (4,3).  The contractor will only install pipes of cost at least 11.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.

【分析】最小生成树(裸)。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 4e3;
const int M = ;
int n,m,k,edg[N][N],lowcost[N],pre[N];
void Prim() {
for(int i=;i<=n;i++){
lowcost[i]=edg[][i];
}
lowcost[]=-;
int sum=;
for(int i=;i<n;i++){
int minn=;
for(int j=;j<=n;j++){
if(lowcost[j]!=-&&lowcost[j]<minn){
minn=lowcost[j];
k=j;
}
}
if(minn>=){
puts("-1");
return;
}
sum+=minn;
lowcost[k]=-;
for(int j=;j<=n;j++){
if(edg[j][k]<lowcost[j]){
lowcost[j]=edg[j][k];
}
}
}
printf("%d\n",sum);
}
int main()
{
for(int i=;i<N;i++)for(int j=;j<N;j++)edg[i][j]=;
int u,v,x[N],y[N];
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d%d",&x[i],&y[i]);
}
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
int s=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
if(s>=m)edg[i][j]=edg[j][i]=s;
}
}
Prim();
return ;
}

(寒假集训)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. BZOJ3479: [Usaco2014 Mar]Watering the Fields

    3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 81  Solved: ...

  4. BZOJ 3479: [Usaco2014 Mar]Watering the Fields( MST )

    MST...一开始没注意-1结果就WA了... ---------------------------------------------------------------------------- ...

  5. bzoj 3479: [Usaco2014 Mar]Watering the Fields

    3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 174  Solved ...

  6. CSU-ACM寒假集训选拔-入门题

    CSU-ACM寒假集训选拔-入门题 仅选择部分有价值的题 J(2165): 时间旅行 Description 假设 Bobo 位于时间轴(数轴)上 t0 点,他要使用时间机器回到区间 (0, h] 中 ...

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

    P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...

  8. BZOJ 3479: [Usaco2014 Mar]Watering the Fields(最小生成树)

    这个= =最近刷的都是水题啊QAQ 排除掉不可能的边然后就最小生成树就行了= = CODE: #include<cstdio>#include<iostream>#includ ...

  9. 【2018寒假集训Day 8】【最小生成树】Prim和Kruskal算法模板

    Luogu最小生成树模板题 Prim 原理与dijkstra几乎相同,每次找最优的点,用这个点去松弛未连接的点,也就是用这个点去与未连接的点连接. #include<cstdio> #in ...

随机推荐

  1. HUAWEI TAG-AL00 找IMEI的过程

    前几天,遇到一台华为机型,IMEI获取有问题,然后就找了一下. 以下是解决过程,权当记录一下,尽管为知笔记已经有备份了 :) 0x01: 起因 测试小哥发现,一台机型IMEI获取不全,有问题,拨号页面 ...

  2. 21、AngularJs知识点总结 part-3

    1.选择框select 在 AngularJS 中我们可以使用 ng-option 指令来创建一个下拉列表,列表项通过对象和数组循环输出,也可以使用ng-repeat 指令来创建下拉列表: 区别:ng ...

  3. Linux认知之旅【04 进一步了解目录】!

    一.目录是什么? 二.不得不提的文件系统! 三.绝对路经,相对路径

  4. 聊聊、Spring ServletContainerInitializer

    我们平时用 Java 注解很多,例如 @Configuration.@Component.@Service,我们习惯于通过 XML 方式来实现 Web,而用 Java 注解方式来实现 Web 却很少. ...

  5. React01

    目录 React-day01 入门知识 React介绍 官网 React开发环境初始化 SPA 脚手架初始化项目(方便,稳定)* 通过webpack进行初始化 配置镜像地址 开发工具配置 元素渲染 组 ...

  6. springboot07 mysql02

    多表关系 一.表关系介绍 1. 表之间为什么要有关系 一般来讲,通常都是一张表某一类型数据,比如学生数据存储在学生表,教师数据存储在教师表,学科数据存储在学科表.但是有时候我们需要表示一个学生属于哪一 ...

  7. EasyUi DataGrid 请求Url两次问题

    easyui datagrid 1.4 当total为0时,请求两次url问题 框架问题:需要在easyui文件后加修补补丁 /** * The Patch for jQuery EasyUI 1.4 ...

  8. 八、ISP 接口隔离原则

    ISP应用的场景是某些类不符合SRP原则,但使用这些类的客户端应该根据它们的父类来使用(我感觉这句话应该改为:客户端应该根据它们的抽象类\接口来使用它们),而不是直接使用它们. 定义: 客户端不应该依 ...

  9. Android记事本开发02

    今天: 继续学习基础知识. 昨天: 学习了ADB工具的基本命令. Android项目的目录结构. AndroidManifest.xml Android应用程序的打包和安装 遇到的问题: 无.

  10. struct&&class 空的大小

    #include using namespace std; class ClassA { }; class ClassB { private: int b; }; class ClassC : pub ...