题目描述
Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?输入格式
The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point.
Input contains multiple test cases. Process to the end of file.输出格式
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.
输入样例
3
1.0 1.0
2.0 2.0
2.0 4.0输出样例
3.41
分析
题目大意是给出n个点的坐标,求将所有点连接在一起的最短距离,即求最小生成树。
以下是分别用堆优化的Prim算法和Kruskal算法写成的源代码。
源程序
Prim算法
#include <bits/stdc++.h>
#define MAXN 105
#define INF 0x3f3f3f3f
using namespace std;
struct Edge{
int v;
double w;
bool operator <(const Edge a)const{
return w>a.w;
}
};
int n;
double dis[MAXN],x[MAXN],y[MAXN],g[MAXN][MAXN];
bool vis[MAXN];
void init()
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
g[i][j]=INF;
}
double getdis(int i,int j)
{
return (x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
}
double prim()
{
priority_queue<Edge> q;
for(int i=1;i<=n;i++)dis[i]=INF,vis[i]=false; //初始化
dis[1]=0;
q.push(Edge{1,0});
while(!q.empty()){
int u=q.top().v;
q.pop();
if(vis[u])continue; //已加入最小生成树
vis[u]=true; //加入最小生成树
for(int i=1;i<=n;i++){
if(!vis[i]&&dis[i]>g[u][i]){ //还没加入最小生成树且有更小值
dis[i]=g[u][i];
q.push(Edge{i,dis[i]});
}
}
}
double ans=0; //最小生成树权值
for(int i=1;i<=n;i++)
ans+=dis[i];
return ans;
}
int main()
{
while(~scanf("%d",&n)){
init(); //初始化图
for(int i=1;i<=n;i++) //读入坐标
scanf("%lf%lf",&x[i],&y[i]);
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
g[i][j]=g[j][i]=sqrt(getdis(i,j));
printf("%.2lf\n",prim());
}
}
Kruskal算法
#include <bits/stdc++.h>
#define MAXN 105
#define INF 0x3f3f3f3f
using namespace std;
struct Edge{
double w;
int u,v;
bool operator <(const Edge a)const{
return w>a.w;
}
};
int n,father[MAXN];
double x[MAXN],y[MAXN];
priority_queue<Edge> q;
void init() //初始化
{
for(int i=1;i<=n;i++)father[i]=i;
while(!q.empty())q.pop();
}
int find(int x) //并查集
{
if(x==father[x])return x;
return father[x]=find(father[x]);
}
double getdis(int i,int j) //计算距离
{
return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
double kruskal()
{
int k=0; //记录边数
double ans=0; //记录权值
while(k<n-1){
int u=q.top().u,v=q.top().v;
double w=q.top().w;
q.pop();
if(find(u)!=find(v)){ //u,v还没连接
ans+=w; //更新权重
k++; //更新边数
father[find(u)]=find(v);
}
}
return ans;
}
int main()
{
while(~scanf("%d",&n)){
init(); //初始化
for(int i=1;i<=n;i++) //读入点
scanf("%lf%lf",&x[i],&y[i]);
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++){
Edge tmp;
tmp.u=i,tmp.v=j,tmp.w=getdis(i,j);
q.push(tmp);
}
printf("%.2lf\n",kruskal());
}
}