Eddy's picture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8103 Accepted Submission(s): 4103
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
题目意思是说:一张纸上有很多点,让这些点连接起来,不一定直接相连。现在给出这些点的坐标,求连通它们的最短路径是多长。
赤裸裸的最小生成树,我用的是prim算法。
代码如下:
<span style="font-size:12px;">#include<cstdio>
#include<cstring>
#include<cmath>
#define INF 0x3f3f3f
int n;
double map[110][110],sum;
void prim()
{
sum=0;
int i,j,next,visit[110];
double min,lowcost[110];
memset(visit,0,sizeof(visit));
for(i=1;i<=n;++i)
lowcost[i]=map[1][i];
visit[1]=1;
for(i=2;i<=n;++i)
{
min=INF;
for(j=1;j<=n;++j)
{
if(!visit[j]&&min>lowcost[j])
{
min=lowcost[j];
next=j;
}
}
sum+=min;
visit[next]=1;
for(j=1;j<=n;++j)
{
if(!visit[j]&&lowcost[j]>map[next][j])
lowcost[j]=map[next][j];
}
}
printf("%.2lf\n",sum);
}
int main()
{
int i,j;
double x[110],y[110],d;
while(scanf("%d",&n)!=EOF)
{
memset(map,INF,sizeof(map));
for(i=1;i<=n;++i)
scanf("%lf%lf",&x[i],&y[i]);
for(i=1;i<n;++i)//计算并记录点与点间的距离
{
for(j=i+1;j<=n;++j)
{
d=sqrt((x[j]-x[i])*(x[j]-x[i])+(y[j]-y[i])*(y[j]-y[i]));
map[i][j]=map[j][i]=d;
}
}
prim();
}
return 0;
} </span>