首页 > ACM题库 > HDU-杭电 > HDU 1162 Eddy’s picture-最小生成树-[解题报告] C++
2013
12-03

HDU 1162 Eddy’s picture-最小生成树-[解题报告] C++

Eddy’s picture

问题描述 :

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个点全部处理一下,求出每对点之间的长度,然后存入邻接矩阵。然后就KO了。。

这道题悲剧的地方在于最大值赋值的时候出现了错误,在比赛里提交编译错误,在外面提交就AC了。问了N个人也没能解决。

原来是最大值越界了。

以后要用#include<climits>里面的INT_MAX来给变量赋最大值。这样就不会出错了,因为是系统变量。。。。

代码如下:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<climits> //INT_MAX的头文件
#include<algorithm>
using namespace std;

#define MAXN 110
double map[MAXN][MAXN], lowcost[MAXN];
bool visit[MAXN];
double sum;

struct point
{
	double x, y;
};
point dian[MAXN];

double fun(const point x, const point y)
{
	return sqrt((y.y - x.y) * (y.y - x.y) + (y.x - x.x) * (y.x - x.x));
}

void prim(int end)
{
	double temp;
	int i, j, k;
	sum = 0;
	memset(visit, false, sizeof(visit));
	for(i = 1; i <= end; ++i)
		lowcost[i] = map[1][i];
	visit[1] = true;
	for(i = 1; i <= end; ++i)
	{
		temp =INT_MAX; //这里错了N久。。以后就这么用了。。。
		for(j = 1; j <= end; ++j)
			if(!visit[j] && temp > lowcost[j])
			temp = lowcost[k = j];
		if(temp == INT_MAX)
			break;
		visit[k] = true;
		sum += temp;
		for(j = 1; j <= end; ++j)
			if(!visit[j] && lowcost[j] > map[k][j])
				lowcost[j] = map[k][j];
	}
}

int main()
{
	int i, j, num, count;
	double x, y;
	while(scanf("%d", &num) != EOF)
	{
		count = 0;
		for(i = 1; i < MAXN; ++i)
			for(j = 1; j < MAXN; ++j)
				map[i][j] = INT_MAX;
		for(i = 1; i <= num; ++i)
		{
			count++;
			scanf("%lf%lf", &dian[count].x, &dian[count].y);
		}
		for(i = 1; i <= count; ++i)
			for(j = 1; j <= count; ++j)
			{
				if(j == i) continue;
				map[i][j] = min(map[i][j], fun(dian[i], dian[j]));
			}
		prim(count);
		printf("%.2lf\n", sum);
	}
	return 0;
}


  1. 有两个重复的话结果是正确的,但解法不够严谨,后面重复的覆盖掉前面的,由于题目数据限制也比较严,所以能提交通过。已更新算法

  2. Excellent Web-site! I required to ask if I might webpages and use a component of the net web website and use a number of factors for just about any faculty process. Please notify me through email regardless of whether that would be excellent. Many thanks

  3. 我没看懂题目
    2
    5 6 -1 5 4 -7
    7 0 6 -1 1 -6 7 -5
    我觉得第一个应该是5 6 -1 5 4 输出是19 5 4
    第二个是7 0 6 -1 1 -6 7输出是14 7 7
    不知道题目例子是怎么得出来的

  4. 如果两个序列的最后字符不匹配(即X [M-1]!= Y [N-1])
    L(X [0 .. M-1],Y [0 .. N-1])= MAX(L(X [0 .. M-2],Y [0 .. N-1]),L(X [0 .. M-1],Y [0 .. N-1])
    这里写错了吧。