Description:

In China, foreign brand commodities are often much more expensive than abroad. The main reason is that we Chinese people tend to think foreign things are better and we are willing to pay much for them. The typical example is, on the United Airline flight, they give you Haagendazs ice cream for free, but in China, you will pay $10 to buy just a little cup.
So when we Chinese go abroad, one of our most favorite activities is shopping in outlets. Some people buy tens of famous brand shoes and bags one time. In Las Vegas, the existing outlets can’t match the demand of Chinese. So they want to build a new outlets in the desert. The new outlets consists of many stores. All stores are connected by roads. They want to minimize the total road length. The owner of the outlets just hired a data mining expert, and the expert told him that Nike store and Apple store must be directly connected by a road. Now please help him figure out how to minimize the total road length under this condition. A store can be considered as a point and a road is a line segment connecting two stores.

Input:

There are several test cases. For each test case: The first line is an integer N( 3 <= N <= 50) , meaning there are N stores in the outlets. These N stores are numbered from 1 to N. The second line contains two integers p and q, indicating that the No. p store is a Nike store and the No. q store is an Apple store. Then N lines follow. The i-th line describes the position of the i-th store. The store position is represented by two integers x,y( -100<= x,y <= 100) , meaning that the coordinate of the store is (x,y). These N stores are all located at different place. The input ends by N = 0.

Output:

For each test case, print the minimum total road length. The result should be rounded to 2 digits after decimal point.

Sample Input:

4
2 3
0 0
1 0
0 -1
1 -1
0

Sample Output:

3.41

题目链接

给出一些点的坐标,求最小生成树,其中指定两个点必须直接相连。

在计算点与点之间边的权值时将两个指定点之间边的权值设置为0,这样就可以把两个点看为一个点,这时再用Prim算法从其中一个点开始求最小生成树,把所有最小生成树中的权值求和,再加上两个指定点之间的距离即为答案。

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const int maxn = 110;
const int mod = 1e9+7;
const double eps = 1e-8;
const double pi = asin(1.0)*2;
const double e = 2.718281828459;
void fre() {
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
}

struct locate {
	double x, y;
};

int n;
int p, q;
double NADis;
double ans;
locate co[maxn];
double dis[maxn];
bool vis[maxn];
double maze[maxn][maxn];

void Prim() {
	for (int i = 0; i < maxn; ++i) {
		dis[i] = INF;
	}
	mem(vis, 0);
	dis[p] = 0;
	ans = 0;
	for (int i = 1; i <= n; ++i) {
		int u = -1;
		double min = INF;
		for (int j = 1; j <= n; ++j) {
			if (!vis[j] && dis[j] < min) {
				u = j;
				min = dis[j];
			}
		}
		vis[u] = 1;
		ans += min;
		for (int j = 1; j <= n; ++j) {
			if (maze[u][j] != INF) {
				if (!vis[j] && maze[u][j] < dis[j]) {
					dis[j] = maze[u][j];
				}
			}
		}
	}
}

int main() {
	//fre();
	while (scanf("%d", &n)) {
		if (!n) {
			break;
		}
		scanf("%d%d", &p, &q);
		for (int i = 1; i <= n; ++i) {
			scanf("%lf%lf", &co[i].x, &co[i].y);
		}
		if (p > q) {
			int temp = q;
			q = p;
			p = temp;
		}
		for (int i = 0; i < maxn; ++i) {
			for (int j = 0; j < maxn; ++j) {
				maze[i][j] = INF;
			}
		}
		for (int i = 1; i <= n; ++i) {
			for (int j = i + 1; j <= n; ++j) {
				maze[i][j] = maze[j][i] = sqrt((co[i].x - co[j].x) * (co[i].x - co[j].x) + (co[i].y - co[j].y) * (co[i].y - co[j].y));
			}
		}
		maze[p][q] = maze[q][p] = 0;
		Prim();
		NADis = sqrt((co[p].x - co[q].x) * (co[p].x - co[q].x) + (co[p].y - co[q].y) * (co[p].y - co[q].y));
		printf("%.2lf\n", ans + NADis);
	}
    return 0;
}