题意:最小生成树的模板题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 120;
const int INF = 0x3f3f3f3f;
double x[MAXN],y[MAXN],arc[MAXN][MAXN],low[MAXN];
int N;
int in[MAXN];
double dist(int i,int j){
return (x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
}
double Prim_MST(){
int p,i,j;
double Min,cost=0.0;
memset(in,0,sizeof(in));
for (i = 1; i < N; i++)
low[i] = arc[0][i];
for (i = 1; i < N; i++){
Min = INF;
for (j = 1; j < N; j++)
if (!in[j] && Min > low[j]){
p = j;
Min = low[j];
}
if (Min == INF)
return -1;
in[p] = 1;
cost += sqrt(Min);
for (j = 1; j < N; j++)
if (!in[j] && low[j]>arc[p][j])
low[j] = arc[p][j];
}
return cost;
}
int main(){
int t;
scanf("%d",&t);
while (t--){
scanf("%d",&N);
for (int i = 0; i < N; i++)
scanf("%lf%lf",&x[i],&y[i]);
for (int i = 0; i < N; i++)
for (int j = 0; j < i; j++)
arc[i][j] = arc[j][i] = dist(i,j);
printf("%.2lf\n",Prim_MST());
if (t)
printf("\n");
}
return 0;
}