Swordfish (最小生成树——克鲁斯卡尔方法)

 There exists a world within our world
A world beneath what we call cyberspace.
A world protected by firewalls,
passwords and the most advanced
security systems.
In this world we hide
our deepest secrets,
our most incriminating information,
and of course, a shole lot of money.
This is the world of Swordfish.

  We all remember that in the movie Swordfish, Gabriel broke into the World Bank Investors Group in West Los Angeles, to rob $9.5 billion. And he needed Stanley, the best hacker in the world, to help him break into the password protecting the bank system. Stanley's lovely daughter Holly was seized by Gabriel, so he had to work for him. But at the last moment, Stanley made some little trick in his hacker mission: he injected a trojan horse in the bank system, so the money would jump from one account to another account every 60 seconds, and would continue jumping in the next 10 years. Only Stanley knew when and where to get the money. If Gabriel killed Stanley, he would never get a single dollar. Stanley wanted Gabriel to release all these hostages and he would help him to find the money back.
  You who has watched the movie know that Gabriel at last got the money by threatening to hang Ginger to death. Why not Gabriel go get the money himself? Because these money keep jumping, and these accounts are scattered in different cities. In order to gather up these money Gabriel would need to build money transfering tunnels to connect all these cities. Surely it will be really expensive to construct such a transfering tunnel, so Gabriel wants to find out the minimal total length of the tunnel required to connect all these cites. Now he asks you to write a computer program to find out the minimal length. Since Gabriel will get caught at the end of it anyway, so you can go ahead and write the program without feeling guilty about helping a criminal.

Input:
The input contains several test cases. Each test case begins with a line contains only one integer N (0 <= N <=100), which indicates the number of cities you have to connect. The next N lines each contains two real numbers X and Y(-10000 <= X,Y <= 10000), which are the citie's Cartesian coordinates (to make the problem simple, we can assume that we live in a flat world). The input is terminated by a case with N=0 and you must not print any output for this case.

Output:
You need to help Gabriel calculate the minimal length of tunnel needed to connect all these cites. You can saftly assume that such a tunnel can be built directly from one city to another. For each of the input cases, the output shall consist of two lines: the first line contains "Case #n:", where n is the case number (starting from 1); and the next line contains "The minimal distance is: d", where d is the minimal distance, rounded to 2 decimal places. Output a blank line between two test cases.

Sample Input:

5
0 0
0 1
1 1
1 0
0.5 0.5
0


Sample Output:

Case #1:
The minimal distance is: 2.83

题意:多组输入,每组给N个点的坐标,求把这些点连起来的最短距离。

代码:

#include<stdio.h>
#include<cstring>
#include<iostream>
#include<queue>
#include<cstring>
#include<vector>
#include<set>
#include<algorithm>
#include<cmath>

#define INF 0x3f3f3f3f

using namespace std;

struct D   //存边的信息 
{
	double len;
	int head,tail;
	D(int a,int b,double mid)
	{
		head = a;
		tail = b;
		len = mid;
	}
};

struct cmp
{
	bool operator()(D a,D b)
	{
		return a.len>b.len;
	}
};

int pre[105];
int flag;
double board[105][2];
int edge;
double lenth;

int find(int a)  //找祖先 
{
	if(pre[a] == a)return a;
	return pre[a] = find(pre[a]);
}

int judge(int a,int b) //判断是在一个集合 
{
	int A = find(a);
	int B = find(b);
	if(A != B)
	{
		pre[A] = B;
		return 1;
	}
	return 0;
}

int main()
{
	int N;
	int key = 1;
	while(scanf("%d",&N)&&N)
	{
		if(key)key--;
		else printf("\n");//注意格式 
		lenth = 0;
		for(int i=0 ; i<=N ; i++)pre[i] = i;

		edge = 0;

		priority_queue<D,vector<D>,cmp>Q;

		for(int i=0 ; i<N ; i++)
		{
			scanf("%lf %lf",&board[i][0],&board[i][1]);
		}
		for(int i=0 ; i<N ; i++)
		{
			for(int j=i+1 ; j<N ; j++)
			{
				double len = sqrt(pow((board[i][0]-board[j][0]),2)+pow((board[i][1]-board[j][1]),2));
				Q.push(D(i,j,len));         // 把每条边的信息计算出来然后入队 
			}
		}
		while(edge < N-1)  //找到N-1条边后结束 
		{
			D mid = Q.top();
			Q.pop();
			if(judge(mid.head,mid.tail))
			{
				edge++;
				lenth += mid.len;
			}
		}
		printf("Case #%d:\n",++flag);
		printf("The minimal distance is: %.2f\n",lenth);
	}
	return 0;
}


posted @ 2017-08-01 21:33  Assassin_poi君  阅读(254)  评论(0编辑  收藏  举报