链接:http://acm.hdu.edu.cn/showproblem.php?pid=1875
源代码:
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
#define INF 999999
#define N 110
double edge[N][N];
double dist[N];
int visited[N];
int b[N][2];
int flag;
void prime(double edge[][N],int n)
{
memset(visited,0,sizeof(visited));
int i,j,temp;
double min;
for(i=2;i<=n;i++)
dist[i]=edge[1][i];
dist[1]=0.0;
visited[1]=1;
double sum=0.0;
for(i=2;i<=n;i++)
{
min=INF*1.0;
flag=0;
for(j=1;j<=n;j++)
{
if(!visited[j]&&dist[j]<min)
{
min=dist[j];
temp=j;
flag=1;
}
}
if(!flag)break;
sum+=min;
visited[temp]=1;
for(int k=1;k<=n;k++)
{
if(!visited[k]&&edge[temp][k]<dist[k])
{
dist[k]=edge[temp][k];
}
}
}
if(!flag)printf("oh!\n");
else printf("%.1lf\n",sum*100);
}
int main()
{
int cases,x,y;
scanf("%d",&cases);
while(cases--)
{
int c,i,j;
scanf("%d",&c);
for(i=1;i<=c;i++)
for(j=1;j<=c;j++)
edge[i][j]=INF;
for(i=1;i<=c;i++)
{
scanf("%d%d",&x,&y);
b[i][0]=x;
b[i][1]=y;
}
for(int i1=1;i1<c;i1++)
{
for(int j1=i1+1;j1<=c;j1++)
{
double dis=sqrt((b[i1][0]-b[j1][0])*(b[i1][0]-b[j1][0])*1.0+(b[i1][1]-b[j1][1])*(b[i1][1]-b[j1][1])*1.0);
if(dis>=10&&dis<=1000)
{
edge[i1][j1]=dis;
edge[j1][i1]=dis;
}
}
}
prime(edge,c);
}
//system("pause");
return 0;
}