Offer 驾到,掘友接招!我正在参与2022春招打卡活动,点击查看活动详情。
一、题目描述:
题目描述
2015 年,全中国实现了户户通电。作为一名电力建设者,小明正在帮助一带一路上的国家通电。
这一次,小明要帮助 nn 个村庄通电,其中 1 号村庄正好可以建立一个发电站,所发的电足够所有村庄使用。
现在,这 n 个村庄之间都没有电线相连,小明主要要做的是架设电线连接这些村庄,使得所有村庄都直接或间接的与发电站相通。
小明测量了所有村庄的位置(坐标)和高度,如果要连接两个村庄,小明需要花费两个村庄之间的坐标距离加上高度差的平方。
高度的计算方式与横纵坐标的计算方式不同。
由于经费有限,请帮助小明计算他至少要花费多少费用才能使这 n个村庄都通电。
输入描述
输入的第一行包含一个整数 n ,表示村庄的数量。
接下来 n 行,每个三个整数 x, y,h,分别表示一个村庄的横、纵坐标和高度,其中第一个村庄可以建立发电站。
其中,
1≤n≤1000,
0≤x,y,h≤10000。
输出描述
输出一行,包含一个实数,四舍五入保留 2 位小数,表示答案。
输入输出样例
示例
输入
4 1 1 3 9 9 7 8 8 6 4 5 4输出
17.41运行限制
- 最大运行时间:1s
- 最大运行内存: 256M
二、思路分析:
这一题就是经典的最短路问题。其要点在于我们可以把这一题看做所有点都相互存在联系,所以边的数量为n!,是稠密图,边的距离为两个村庄之间的坐标距离加上高度差的平方。注意要使用double否则会存在因为精度不够而出现的错误。
三、AC 代码:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static int n;
static double sum;
static double dist[] = null;
static boolean st[] = null;
static Chun[] list = null;
static double[][] g = null;
static class Chun{
int x,y,h;
public Chun(int x,int y,int h){
this.x = x;
this.y = y;
this.h = h;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n = in.nextInt();
dist = new double[ n + 1 ]; st = new boolean[ n + 1 ];
list = new Chun[ n + 1 ]; g = new double[n+1][n+1];
Arrays.fill(dist, Double.MAX_VALUE/2);
for (int i = 1; i <= n; i++) {
list[i] = new Chun(in.nextInt(), in.nextInt(), in.nextInt());
}
for (int i = 1; i < n+1; i++) {
for (int j = 1; j < n+1; j++) {
if (i!=j) {
g[i][j] = g[j][i] = Math.sqrt(Math.pow((list[i].x - list[j].x),2) + Math.pow((list[i].y - list[j].y),2))
+ Math.pow((list[i].h - list[j].h),2);
}
}
}
prim();
System.out.println(String.format("%.2f",sum));
}
private static void prim() {
for (int i = 0; i < n; i++) {
int t = -1;
for (int j = 1; j < n+1; j++) {
if (!st[j] && (t == -1 || dist[t] > dist[j])) {
t = j;
}
}
if(i > 0) sum+=dist[t];
for (int j = 1; j < n+1; j++) {
dist[j] = Math.min(dist[j], g[t][j]);
}
st[t] = true;
}
}
}