您的位置:首页 > 其它

HDU-1162-Eddy's picture

2017-08-04 11:01 288 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162


Eddy's picture

Problem Description

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?

 

Input

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.

 

Output

Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points. 

 

Sample Input

3
1.0 1.0
2.0 2.0
2.0 4.0

 

Sample Output

3.41

 

给出n个点的坐标,问使他们连通的最短距离。
prim算法

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
double dis[111],e[111][111],x[111],y[111];
int visit[111];
const double inf=999999;
int n;
void init()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j)
e[i][j]=0;
else
e[i][j]=inf;
}
}
}
void prim()
{
int i,j,k;
for(i=1;i<=n;i++)
{
dis[i]=e[1][i];
}
memset(visit,0,sizeof(visit));
visit[1]=1;
double min,sum=0.0;
int count=1;
while(count<n)
{
min=inf;
for(i=1;i<=n;i++)
{
if(visit[i]==0&&dis[i]<min)
{
min=dis[i];
j=i;
}
}
visit[j]=1;
count++;
sum+=dis[j];
for(k=1;k<=n;k++)
{
if(visit[k]==0&&dis[k]>e[j][k])
dis[k]=e[j][k];
}
}
printf("%.2lf\n",sum);
}
int main()
{
while(cin>>n)
{
int i,j;
if(n==0)
break;
for(i=1;i<=n;i++)
{
cin>>x[i]>>y[i];
}
init();
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==j)
continue;
double ww=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
e[i][j]=ww;
}
}
prim();
}
return 0;
}

kruskal算法
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int pre[111];
int n;
struct edge{
double u,v,w;
}e[11111];
void init()
{
for(int i=1;i<=n;i++)
pre[i]=i;
}
int find(int r)
{
if(pre[r]==r)
return r;
else
{
pre[r]=find(pre[r]);
return pre[r];
}
}
int merge(int u,int v)
{
int t1=find(u);
int t2=find(v);
if(t1!=t2)
{
pre[t2]=t1;
return 1;
}
return 0;
}
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
int main()
{
int i,j;
double x[111],y[111];
while(cin>>n)
{
for(i=1;i<=n;i++)
cin>>x[i]>>y[i];
init();
int m=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
double ww=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
e[m].u=i;
e[m].v=j;
e[m].w=ww;
m++;
}
}
sort(e+1,e+m,cmp);//注意这是e+m
int count=0;
double sum=0.0;
for(i=1;i<m;i++)//循环到m-1就好了
{
if(merge(e[i].u,e[i].v))
{
count++;
sum+=e[i].w;
}
}
if(count==n-1)
printf("%.2lf\n",sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: