1 题目编号:1002
2 题目内容:
3 1.0 1.0 2.0 2.0 2.0 4.0
3.41
4 代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
struct Node
{
double x;
double y;
}A[110];
double G[110][110], low[110];
int vis[110];
void Prim(int N)
{
memset(vis, 0, sizeof(vis));
int pos = 1;
vis[pos] = 1;
double ans = 0;
for (int i = 1; i <= N; i++)
if (i != pos)
low[i] = G[pos][i];
for (int i = 1; i < N; i++)
{
double Min = 0x7ffffff;
for (int j = 1; j <= N; j++)
{
if (!vis[j] && Min > low[j])
{
Min = low[j];
pos = j;
}
}
vis[pos] = 1;
ans += Min;
for (int j = 1; j <= N; j++)
{
if (!vis[j] && low[j] > G[pos][j])
low[j] = G[pos][j];
}
}
printf("%.2lf\n", ans);
}
int main()
{
int N;
double Dist, x, y;
while (cin >> N)
{
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= N; j++)
G[i][j] = 0x7fffff;
}
memset(A, 0, sizeof(A));
for (int i = 1; i <= N; i++)
{
cin >> A[i].x >> A[i].y;
}
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= N; j++)
{
x = A[i].x - A[j].x;
y = A[i].y - A[j].y;
Dist = sqrt(x*x + y*y);
G[i][j] = Dist;
}
}
Prim(N);
}
return 0;
}