2021-08-03

20:31:13

链接:

https://www.luogu.com.cn/problem/P2212

题目详情:

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 个点,第 i 个点的坐标为 (xi​,yi​),如果想连通第 i 个点与第 j 个点,需要耗费的代价为两点的距离。第 i 个点与第 j 个点之间的距离使用欧几里得距离进行计算,即:

(xi​−xj​)^2+(yi​−yj​)^2

我们规定耗费代价小于 c 的两点无法连通,求使得每两点都能连通下的最小代价,如果无法连通输出 -1

输入格式

* Line 1: The integers N and C.

* Lines 2..1+N: Line i+1 contains the integers xi and yi.

第一行两个整数 n,cn,c 代表点数与想要连通代价不能少于的一个数。
接下来 n 行每行两个整数 xi​,yi​ 描述第 i 个点。

输出格式

* Line 1: The minimum cost of a network of pipes connecting the

fields, or -1 if no such network can be built.

一行一个整数代表使得每两点都能连通下的最小代价,如果无法连通输出 -1

输入输出样例

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

数据规模与约定

对于 100% 的数据,1≤n≤2000,0≤xi​,yi​≤1000,1≤c≤10^6。

题目分析:

这是一道prim算法题,我们可以利用prim算法的模板,加上题目的限制条件两点距离不得小于c,就可以解出该题。

话不多说直接上代码吧

#include<iostream>
#include<cmath>
using namespace std;
const int N=2005;
const int INF=1e8;
int n,c;
struct Node{
int x,y;
}node[N];
int g[N][N];//每个点之间的距离
int dist[N];
bool st[N];
int distance(int x1,int y1,int x2,int y2){
int t=pow((x1-x2),2)+pow((y1-y2),2);
return t;
} int Prim(){
int res=0;
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
int x1=node[i].x,y1=node[i].y,x2=node[j].x,y2=node[j].y;
int t=distance(x1,y1,x2,y2);
if(t>=c)g[i][j]=g[j][i]=t;
else g[i][j]=g[j][i]=INF;
}
}
for(int i=0;i<n;i++){
int t=-1;
for(int j=0;j<n;j++){
if(!st[j]&&(t==-1||dist[t]>dist[j]))t=j;
}
if(i&&dist[t]==INF)return -1;
if(i)res+=dist[t];
st[t]=true;
for(int j=1;j<n;j++)
dist[j]=min(dist[j],g[t][j]);
}
return res;
} int main()
{
cin>>n>>c;
for(int i=0;i<n;i++){
int x,y;
cin>>x>>y;
node[i]={x,y};
dist[i]=INF;
g[i][i]=INF;
}
int res=Prim();
cout<<res;
return 0;
}

 

2021-08-03

20:43:34

洛谷 P2212 [USACO14MAR]Watering the Fields S 题解的更多相关文章

  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

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

  4. 洛谷P1484 种树&洛谷P3620 [APIO/CTSC 2007]数据备份 题解(堆+贪心)

    洛谷P1484 种树&洛谷P3620 [APIO/CTSC 2007]数据备份 题解(堆+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/132 ...

  5. 洛谷P3387 【模板】缩点 题解

    背景 今天\(loj\)挂了,于是就有了闲情雅致来刷\(luogu\) 题面 洛谷P3387 [模板]缩点传送门 题意 给定一个\(n\)个点\(m\)条边有向图,每个点有一个权值,求一条路径,使路径 ...

  6. [NOI导刊2010提高&洛谷P1774]最接近神的人 题解(树状数组求逆序对)

    [NOI导刊2010提高&洛谷P1774]最接近神的人 Description 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某 ...

  7. [洛谷P1029]最大公约数与最小公倍数问题 题解(辗转相除法求GCD)

    [洛谷P1029]最大公约数与最小公倍数问题 Description 输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P, ...

  8. BZOJ5288 & 洛谷4436 & LOJ2508:[HNOI/AHOI2018]游戏——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=5288 https://www.luogu.org/problemnew/show/P4436 ht ...

  9. BZOJ4943 & 洛谷3823 & UOJ315:[NOI2017]蚯蚓排队——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4943 http://uoj.ac/problem/315 https://www.luogu.or ...

  10. BZOJ1229 & 洛谷2917:[USACO2008 NOV]toy 玩具 & 洛谷4480:[BJWC2018]餐巾计划问题——题解

    标题很长emmm…… [USACO2008 NOV]toy 玩具 https://www.luogu.org/problemnew/show/P2917 https://www.lydsy.com/J ...

随机推荐

  1. yaml文件读取转化为类

    首先你要有一个文件读取的方法,写一个根据传入路径 + 类来自动返回对应类的方法. /** * 根据传入的path,加载配置文件内容到对应class中 */ public static <T> ...

  2. Verilog仿真遇到的问题

    1.Vivado 15.4仿真时编译没有报错,但是仿真不成功,逻辑很简单,full为高电平时,rd_en要拉高,但全程没有拉高! 检查语法发现语句" else if( empty == 'b ...

  3. Ubuntu16.04系统语言设置为中文以及搜狗输入法的安装

    特别声明:本文是在操作完才做的记录,不是特别详细,见谅哈! 虚拟机安装的Ubuntu16.04结果语言设置只有英文...起初没啥影响,后来发现自己的脚本注释显示全乱码,而且直接影响脚本运行(其实可能是 ...

  4. AIIE21th003: 2021年第二届国际工业工程和人工智能大会(IEAI 2021)

    会议亮点: 1.IEAI2021由ACM新加坡分会赞助举办,它是MSIE 2021的分会. 2.被接收的文章会被发表在ACM国际论文集上,并被EI Compendex, Scopus, Thomson ...

  5. Java-面向对象基础 this& 重载

    1.this表示当前对象 获取当前对象的属性 使用this调用当前属性 2.重载 如果两个方法的方法名相同,但参数不一致,那么可以说一个方法是另一个方法的重载

  6. HDLbits—— 3-input look-up-table

    // a 3-input look-up-table // In this question, you will design a circuit for an 8x1 memory, // wher ...

  7. ctfshow CRYPTO RSA系列

    ctfshow CRYPTO RSA系列 目录 ctfshow CRYPTO RSA系列 babyRSA 分析 解题 esayrsa1 分析 解题 esayrsa2 分析 解题 esayrsa3 分析 ...

  8. Redis-shake工具 [ 自建redis集群->云redis主从 ]

    redis-shake工具是阿里用go写的开源工具 开始前准备 1. 确保ECS实例与Redis实例属于同一专有网络(即实例基本信息中的专有网络ID一致) 2. 获取ECS实例的内网IP地址,即执行操 ...

  9. CG3Y/ CG3Z 程序迁移

    /usr/sap/trans/cofiles/K900075.S4Q 固定值  文件目录(AL11)下去找 C:\Users\lenovo\Desktop\publici\K900075.S4Q /u ...

  10. 供配电一次测 PT柜 解释

    文章来源: PT柜_百度百科 (baidu.com)  视频介绍 很多电工不清楚的高压PT柜,老电工带你了解工作原理和作用_搜狐汽车_搜狐网 (sohu.com) 很多电工怕PT柜,今天电气成套设计实 ...