poj -2560-Freckles-最小生成树

In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ripley's engagement falls through.

Consider Dick's back to be a plane with freckles at various (x,y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

The first line contains 0 < n <= 100, the number of freckles on Dick's back. For each freckle, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the freckle.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

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

Sample Input

1

3
1.0 1.0
2.0 2.0
2.0 4.0

Sample Output

3.41

其实这个是UVA上的一个题,但是poj 2560 也是一样,就是输出有所区别

题目很简单,但是错了3次,原因是题目没看清楚,两个case之间要空出一行、、坑啊。。

题意:给出坐标,然后用最短的直线连n个点,使得每两个点之间有连线、不就是最小生成树求整个网络的最短距离么、、

#include <stdio.h>
#include <cmath>
#include <iostream>

using namespace std;

#define INF 1 << 29
int n;
double map[101][101];
int vis[101];
double dis[101];
double prim(int x) {
	int i, j, k;
	double ans = 0;
	for(i = 0; i < n; i ++) {
		vis[i] = 0;
		dis[i] = map[x][i];
	}
	vis[0] = 1;
	for(i = 1; i < n; i ++) {
		double min = INF;
		for(j = 0; j < n; j ++)	{
			if(min > dis[j] && !vis[j]) {
				min = dis[j];
				k = j;
			}
		}
		vis[k] = 1;
		ans += min;
		for(j = 0;j < n; j ++) {
			if(dis[j] > map[k][j] && !vis[j]) {
				dis[j] = map[k][j];
			}
		}
	}
	return ans;
}
int main() {
	int i, j;
	int k = 0;
	int cas;
	double x[101];
	double y[101];
	scanf("%d", &cas);
	while (cas -- ) {
		scanf("%d", &n);
		for (i = 0; i < 105; i ++)
			for (j = 0; j < 105; j ++)
				map[i][j] = 0;
			for(i = 0; i < n; i ++)
				scanf("%lf%lf", &x[i], &y[i]);
			for(i = 0; i < n; i ++) {
				for(j = i + 1; j < n; j ++) {
					map[i][j] = map[j][i] = sqrt(double((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])));
				}
			}
			printf("%.2lf\n", prim(0));
			if(cas) printf("\n");
	}
	return 0;
}


你可能感兴趣的:(poj -2560-Freckles-最小生成树)