题目:http://acm.hdu.edu.cn/showproblem.php?pid=1875
/************************************************************************/
/*
hdu 畅通工程再续
有条件的最小生成树
题目大意:在这些小岛中建设最小花费的桥,但是一座桥的距离必须在10 -- 1000之间。
*/
/************************************************************************/ #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h> #define MAX 0xfffffff const int N = ; typedef struct
{
int x,y;
}POINT; POINT p[N];
double map[N][N];
int vis[N];
int T,C,x,y,i,j; double get_distance(POINT a,POINT b)
{
return sqrt(pow(a.x-b.x,2.0)+pow(a.y-b.y,2.0));
} void build_map()
{
double len;
for (int t = ; t < C; t++)
for (int v = t; v < C; v++)
{
len = get_distance(p[t],p[v]);
if(len>= &&len <= )
map[t][v] = map[v][t] = (t==v)?:len;
else map[t][v] = map[v][t] = MAX;
} } void prim()
{
int k,t = C;
double min,sum = ;
memset(vis,,sizeof(vis));
vis[]=;
while(--t)
{
min = MAX;
for (i = ; i < C; i++)
{
if (vis[i]!= && map[][i] < min)
{
min = map[][i];
k = i;
}
}
if (min==MAX)break;
vis[k] = ;
sum += min;
for (i = ; i < C; i++)
{
if (vis[i]!= && map[k][i] < map[][i] )
map[][i] = map[k][i];
}
}
//printf("%d\n",t);
if(t==)
{
printf("%.1f\n",sum*);
}else printf("oh!\n");
} int main()
{
while(scanf("%d",&T)!= EOF)
{
for (int t = ; t < T; t++)
{
scanf("%d",&C);
for (int c = ; c < C; c++)
scanf("%d%d",&p[c].x,&p[c].y);
build_map();
prim();
} }
return ;
}