做本题的同学可以做 1162 Eddy's picture(hud)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
#define MAX 5000
struct dis
{
double x, y;
}N[101];
// 权值 路程总和
double c[MAX], sum;
// 并查集 排序 端点
int p[105], r[MAX], n, m, a[MAX], b[MAX], nc;
double cmp(const int x, const int y)
{
return c[x]<c[y];
}
int find(int n)
{
return p[n]==n?n:find(p[n]);
}
void nKurskal()
{
int i;
for (i=1; i<=n; i++)
p[i] = i;
for (i=1; i<=m; i++)
r[i] = i;
sort(r+1, r+m+1, cmp);
for (i=1; i<=m; i++)
{
int t = r[i];
int x = find(a[t]);
int y = find(b[t]);
if (x!=y && c[t]>=10.0 && c[t]<=1000.0)//条件判断
{
sum += c[t];
p[x] = y;
nc++; //计算是否包含所有的顶点
}
}
}
int main()
{
int i, j, num;
scanf("%d", &num);
while (num--)
{
scanf("%d", &n);
for (i=1; i<=n; i++)
cin>>N[i].x>>N[i].y;
nc = 1;
for (i=1; i<=n; i++)
for (j=i+1; j<=n; j++)
{
a[nc] = i;
b[nc] = j;
c[nc] = sqrt((N[i].x-N[j].x)*(N[i].x-N[j].x)+(N[i].y-N[j].y)*(N[i].y-N[j].y)); //计算距离 权值
nc++;
}
m = n * (n-1) / 2;//村庄数量
sum = 0;
nc = 1;
nKurskal();
if (nc==n)
printf("%.1f\n", sum*100.0);
else
printf("oh!\n");
}
return 0;
}