当前位置:网站首页>Blue Bridge Cup provincial match simulation question 9 (MST)
Blue Bridge Cup provincial match simulation question 9 (MST)
2022-07-05 08:51:00 【Qizi K】
Blue Bridge Cup provincial match simulation Question 9
Problem description
2015 year , All over China, household electricity has been realized . As a Power Builder , Xiao Ming is helping one belt, one road, the other country .
This time, , Xiao Ming wants to help n The village is electrified , among 1 A power station can be built in Village No , The electricity is enough for all the villages .
Now? , this n There are no wires between the villages , Xiao Ming's main task is to set up wires to connect these villages , So that all villages are directly or indirectly connected to power stations .
Xiao Ming measured the location of all the villages ( coordinate ) And height , If you want to connect two villages , Xiao Ming needs to spend the coordinate distance between the two villages plus the square of the height difference , Formally described as coordinates by (x_1, y_1) The height is h_1 The village and coordinates are (x_2, y_2) The height is h_2 The cost of the connection between villages is
sqrt((x_1-x_2)(x_1-x_2)+(y_1-y_2)(y_1-y_2))+(h_1-h_2)*(h_1-h_2).
In the upper form sqrt Denotes the square root in brackets . Notice the position of the brackets , The calculation method of height is different from that of abscissa and ordinate .
Because of the limited funds , Please help Xiao Ming calculate how much it will cost him to make this n All villages are electrified .
Input format
The first line of input contains an integer n , It means the number of villages .
Next n That's ok , Every three integers x, y, h, It means the horizontal of a village 、 Ordinates and heights , The first village could build a power station .
Output format
Output one line , Contains a real number , Rounding reservation 2 Decimal place , Answer .
The sample input
4
1 1 3
9 9 7
8 8 6
4 5 4
Sample output
17.41
Evaluate use case size and conventions
about 30% The evaluation case of ,1 <= n <= 10;
about 60% The evaluation case of ,1 <= n <= 100;
For all profiling use cases ,1 <= n <= 1000,0 <= x, y, h <= 10000.
tips: Bare minimum spanning tree . Note the data range , Go straight up Prim( Because the data is just 1000, And calculate the distance of each point , Use adjacency matrix directly +prim that will do , No heap optimization ( If the edge is still used kruskal Write mst)).
#include<bits/stdc++.h>
using namespace std;
const int inf = 1e9;
int n,cnt,vis[1005];
struct node{
double x,y,h;
}book[1005];
double mp[1005][1005],dis[1005],sum;
double getdis(int i, int j){
return sqrt((book[i].x - book[j].x) * (book[i].x - book[j].x) + (book[i].y - book[j].y) * (book[i].y - book[j].y)) +
(book[i].h - book[j].h) * (book[i].h - book[j].h);
}
int main(){
scanf("%d",&n);
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j)
if(i == j) mp[i][j] = 0;
else mp[i][j] = inf;
}
for(int i = 1; i <= n; ++i)
scanf("%lf%lf%lf",&book[i].x,&book[i].y,&book[i].h);
for(int i = 1; i <= n; ++i){
for(int j = i + 1; j <= n; ++j)
mp[i][j] = mp[j][i] = getdis(i,j);
}
for(int i = 1; i <= n; ++i)
dis[i] = mp[1][i];
vis[1] = 1, cnt++;
//Prim
while(cnt < n){
int minn = inf,u;
for(int j = 1; j <= n; ++j){
if(!vis[j] && dis[j] < minn){
u = j,minn = dis[j];
}
}
vis[u] = 1,cnt++;
sum += dis[u];
for(int v = 1; v <= n; ++v){
if(!vis[v] && dis[v] > mp[u][v])
dis[v] = mp[u][v];
}
}
printf("%.2lf",sum);
return 0;
}
边栏推荐
- kubeadm系列-00-overview
- Low code platform | apaas platform construction analysis
- 【日常訓練--騰訊精選50】557. 反轉字符串中的單詞 III
- TF coordinate transformation of common components of ros-9 ROS
- How to manage the performance of R & D team?
- My university
- 猜谜语啦(2)
- 287. 寻找重复数-快慢指针
- 特征工程
- How many checks does kubedm series-01-preflight have
猜你喜欢

RT-Thread内核快速入门,内核实现与应用开发学习随笔记

Illustration of eight classic pointer written test questions

Halcon color recognition_ fuses. hdev:classify fuses by color

Programming implementation of ROS learning 2 publisher node

Apaas platform of TOP10 abroad

Guess riddles (5)

Hello everyone, welcome to my CSDN blog!

Programming implementation of ROS learning 5-client node

Yolov4 target detection backbone

容易混淆的基本概念 成员变量 局部变量 全局变量
随机推荐
Use and programming method of ros-8 parameters
TypeScript手把手教程,简单易懂
【日常訓練--騰訊精選50】557. 反轉字符串中的單詞 III
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
Several problems to be considered and solved in the design of multi tenant architecture
C语言标准函数scanf不安全的原因
Classification of plastic surgery: short in long long long
Guess riddles (8)
Xrosstools tool installation for X-Series
Lori remote control LEGO motor
特征工程
Guess riddles (10)
Use arm neon operation to improve memory copy speed
Run菜单解析
The combination of deep learning model and wet experiment is expected to be used for metabolic flux analysis
图解八道经典指针笔试题
Business modeling of software model | vision
Guess riddles (142)
猜谜语啦(7)
asp.net(c#)的货币格式化