题目:
Eddy's picture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4367 Accepted Submission(s): 2156
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?
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.
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
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1162
题目大意及思路:
用一笔将几个点连接起来,要求路径最短,其实就是最小生成树,在这里我用kruskal算法来做,其实就是贪心的思想,算出每两点之间的距离,然后再排序,找出最短的且满足要求的边。
代码:
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
double sum = 0;
const int maxn = 100 + 10;
const int maxm = maxn * maxn;
int father[maxn];
int n,m;
void init()
{
for(int i=0; i<maxn; i++)
{
father[i] = i;
}
}
int findroot(int x)
{
if(father[x] != x)
{
father[x] = findroot(father[x]);
}
return father[x];
}
void Union(int a, int b)
{
a = findroot(a);
b = findroot(b);
if(a != b)
{
father[a] = b;
}
}
struct edge
{
int start;
int end;
double value;
const bool operator <(const struct edge &old) const
{
return value < old.value;
}
}edge[maxm];
struct point
{
double x;
double y;
}point[maxn];
void solve()
{
sum = 0;
int count = 0;
for(int i=0; i<m && count<n-1; i++)
{
int a = edge[i].start;
int b = edge[i].end;
if(findroot(a) != findroot(b))
{
sum += edge[i].value;
Union(a,b);
count++;
}
}
printf("%.2lf\n",sum);
}
int main()
{
int i,j;
while(cin>>n)
{
init();//注意要初始化,经常会忘
double a,b;
m = n*(n-1)/2;
for(i=0; i<n; i++)
{
scanf("%lf%lf",&point[i].x,&point[i].y);
}
int k=0;
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
edge[k].start = i; //这里的点与边的记录有点麻烦
edge[k].end = j;
a = point[i].x - point[j].x;
b = point[i].y - point[j].y;
edge[k].value = sqrt(a*a+b*b);
k++;
}
}
sort(edge, edge+k);
solve();
}
return 0;
}