World Islands
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 206 Accepted Submission(s): 78
Please note;
1.An island can be considered as a point.
2.A bridge can be considered as a line segment connecting two islands.
3.A bridge connects with other bridges only at the islands.
For each test case, the first line is an integer n representing the number of islands.(0<n<50)
Then n lines follow. Each line contains two integers x and y( -20 <= x, y <= 20 ) , indicating the coordinate of an island.
2 5 0 0 1 0 18 0 0 1 1 1 3 0 0 1 0 0 1
3.00 1.00
杭州省赛的第一道水题
#include<stdio.h>
#include<math.h>
int x[60],y[60];
double map[60][60];
double sum;
int n;
double prim(int x)
{
double d[60];
bool used[60];
used[x]=true;
d[x]=1<<31-1;
int t=x+1>n?1:x+1;
for(int i=1;i<=n;i++)
if(i!=x)
{
d[i]=map[t][i];
used[i]=false;
}
used[t]=true;
d[t]=0;
int r;
double s=0;
for(int i=1;i<n-1;i++)
{
double minc=1<<31-1;
for(int j=1;j<=n;j++)
if(!used[j]&&d[j]<minc) minc=d[j],r=j;
s+=minc;
if(s>sum) return 100000000.00;
used[r]=true;
for(int j=1;j<=n;j++)
if(!used[j]&&d[j]>map[r][j]) d[j]=map[r][j];
}
return s;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d%d",&x[i],&y[i]);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(i==j) map[i][j]=0.00;
else map[i][j]=sqrt(1.0*(x[i]-x[j])*(x[i]-x[j])+1.0*(y[i]-y[j])*(y[i]-y[j]));
sum=1<<31-1;
for(int i=1;i<=n;i++)
{
double t=prim(i);
if(t<sum) sum=t;
}
printf("%0.2lf/n",sum);
}
}