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
嗯这题就是最小生成树也是模板的题目
其中吧我觉得要处理的关键是这一步
for(i=1;i<=n;i++)
scanf("%lf%lf",&x[i],&y[i]);
k=0;
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
{
k++;
map[k].u=i;
map[k].v=j;
map[k].w=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
就是输入的问题了 刚开始的时候我让map[k]=x[i],map[k]=y[i];这是不可能过的 因为一个double 一个int 并查集都没有进入 后来改了下 哦 利用了i,j 确实也代替了
x[i],y[i],我觉得这一步蛮巧妙地
然后就是格式要处理的问题了 嗯.....直接在后面加一行是不行的 只有一个样例的时候那就不需要换行了 所以这一点也是要注意下的
c++代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define INF 0x3f3f3f3f
int pre[50000];
struct node
{
int u;
int v;
double w;
}map[50000];
double x[105];
double y[105];
bool cmp(node a,node b)
{
return a.w<b.w;
}
int find(int x)
{
if(x==pre[x])
return x;
else
{
pre[x]=find(pre[x]);
return pre[x];
}
}
int merge(int x,int y)
{
int t1=find(x);
int t2=find(y);
if(t1!=t2)
{
pre[t2]=t1;
return 1;
}
return 0;
}
int main()
{
int i,j,k,t=1,n;
while(scanf("%d",&n)!=EOF&&n!=0)
{
double sum=0;
for(i=1;i<=n;i++)
scanf("%lf%lf",&x[i],&y[i]);
k=0;
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
{
k++;
map[k].u=i;
map[k].v=j;
map[k].w=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
sort(map+1,map+k+1,cmp);
for(i=1;i<=k;i++)
pre[i]=i
; int count=0;
for(i=1;i<=k;i++)
{
if(merge(map[i].u,map[i].v))
{
sum=sum+map[i].w;
count++;
}
if(count==n-1)
break;
}
if(t!=1)
printf("\n");
printf("Case #%d:\n",t);
printf("The minimal distance is: %.2f\n",sum);
t++;
}
return 0;
}