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.
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
思路:标准的最小树模板题,我是用Kruskal算法实现的,说白了就是并查集,每次选最短不在同一区域的加入集合。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<vector>
using namespace std;
const int mm=110;
const int nn=5000;
struct node{double x,y;}p[mm];
struct poin{int u,v;double d;};
poin edge[nn];
int root[mm],ran[mm];
int m,pos;
inline double dis(node a,node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int look(int x)
{
if(x!=root[x])root[x]=look(root[x]);
return root[x];
}
void uni(int x,int y)
{
if(ran[x]>ran[y])root[look(y)]=look(x),ran[x]+=ran[y],ran[y]=0;
else root[look(x)]=look(y),ran[y]+=ran[x],ran[x]=0;
}
bool cmp(poin a,poin b)
{
return a.d<b.d;
}
int main()
{ int cas=0;
while(cin>>m&&m)
{
for(int i=0;i<m;i++)
cin>>p[i].x>>p[i].y;
pos=0;
poin w;
for(int i=0;i<m;i++)
for(int j=i+1;j<m;j++)
{
w.u=i;w.v=j;w.d=dis(p[i],p[j]);
edge[pos++]=w;
}
for(int i=0;i<mm;i++)root[i]=i,ran[i]=1;
sort(edge,edge+pos,cmp);
double min_dis=0.0;
for(int i=0;i<pos;i++)
{ w=edge[i];
if(look(w.u)!=look(w.v))min_dis+=w.d,uni(w.u,w.v);
}
if(cas)cout<<"\n";++cas;
cout.setf(ios::fixed);
cout<<"Case #"<<cas<<":\n";
cout<<setprecision(2)<<"The minimal distance is: "<<min_dis<<"\n";
}
}