当前位置:网站首页>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;
}
边栏推荐
- JS asynchronous error handling
- kubeadm系列-00-overview
- Dynamic dimensions required for input: input, but no shapes were provided. Automatically overriding
- Install the CPU version of tensorflow+cuda+cudnn (ultra detailed)
- 使用arm Neon操作,提高内存拷贝速度
- 某公司文件服务器迁移方案
- Typescript hands-on tutorial, easy to understand
- [牛客网刷题 Day4] JZ55 二叉树的深度
- Warning: retrying occurs during PIP installation
- Ecmascript6 introduction and environment construction
猜你喜欢
随机推荐
猜谜语啦(4)
Use and programming method of ros-8 parameters
Guess riddles (5)
Multiple linear regression (sklearn method)
Dynamic dimensions required for input: input, but no shapes were provided. Automatically overriding
Illustrated network: what is gateway load balancing protocol GLBP?
猜谜语啦(11)
Halcon blob analysis (ball.hdev)
微信H5公众号获取openid爬坑记
IT冷知识(更新ing~)
[daily training] 1200 Minimum absolute difference
EA introduction notes
Several problems to be considered and solved in the design of multi tenant architecture
location search 属性获取登录用户名
Hello everyone, welcome to my CSDN blog!
整形的分类:short in long longlong
How apaas is applied in different organizational structures
Latex improve
Typical low code apaas manufacturer cases
Halcon Chinese character recognition