uva 10034 Problem A: Freckles

时间:2024-01-17 17:06:56

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=975

最小生成树。

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 1000
using namespace std;
const double inf=(double)(<<); int t,n;
struct node
{
double x,y;
}p[maxn];
bool vis[maxn];
double dis[maxn];
double g[maxn][maxn];
double ans; double sqr(double x)
{
return x*x;
} void prim()
{
memset(vis,false,sizeof(vis));
for(int i=; i<=n; i++) dis[i]=g[][i];
dis[]=;
vis[]=true;
for(int i=; i<n; i++)
{
double m=inf;
int x;
for(int y=; y<=n; y++) if(!vis[y]&&dis[y]<m) m=dis[x=y];
ans+=m;
vis[x]=true;
for(int y=; y<=n; y++) if(!vis[y]&&dis[y]>g[x][y]) dis[y]=g[x][y];
}
} int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
for(int i=; i<=n; i++)
{
for(int j=i; j<=n; j++)
{
if(i==j) g[i][j]=;
else
g[i][j]=g[j][i]=sqrt(sqr(p[i].x-p[j].x)+sqr(p[i].y-p[j].y));
}
}
ans=;
prim();
printf("%.2lf\n",ans);
if(t) printf("\n");
}
return ;
}