标签:
Description
Input
Output
Sample Input
2 2 10 10 20 20 3 1 1 2 2 1000 1000
Sample Output
1414.2 oh!
************************************注意:不能用int提高精度判定,只能使用double..要不就会WA#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
using namespace std;
const int maxn = 105;
const double oo = 0x7fffffff;
double G[maxn][maxn];
struct point{int x, y;}p[maxn];
double Len(point a, point b)//求两个小岛的距离,不合法返回oo
{
int x = a.x-b.x;
int y = a.y-b.y;
double l = sqrt(x*x + y*y);
if(l>=10.0 && l<=1000.0)
return l;//合法范围
return oo;
}
double Prim(int N)
{
int vis[maxn]={0, 1};
int i, T = N-1;
double dist[maxn], ans = 0;
for(i=1; i<=N; i++)
dist[i] = G[1][i];
while(T--)
{
int k = -1;
double mini = oo;
for(i=1; i<=N; i++)
{
if(!vis[i] && mini > dist[i])
mini = dist[i], k=i;
}
if(k == -1)return -1;
vis[k] = true, ans += mini;
for(i=1; i<=N; i++)if(!vis[i])
dist[i] = min(dist[i], G[k][i]);
}
return ans;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int i, j, N;
scanf("%d", &N);
for(i=1; i<=N; i++)
scanf("%d%d", &p[i].x, &p[i].y);
for(i=1; i<=N; i++)
for(j=i; j<=N; j++)
{
if(j == i)G[i][j] = 0.0;
else G[i][j] = G[j][i] = Len(p[i], p[j]);
}
double ans = Prim(N);
if(ans < 0)
printf("oh!\n");
else
printf("%.1f\n", ans*100 );
}
return 0;}
标签:
原文地址:http://www.cnblogs.com/liuxin13/p/4676170.html