代码:
3 1.0 1.0 2.0 2.0 2.0 4.0
3.41
思路:
计算每两个点之间的距离,然后转化成为畅通工程题。
代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#define LL long long
#define mod 1e9 + 7
const int M = 105;
using namespace std;
struct node{
int x;
int y;
double value;
}a[M * M >> 1];
int cun[M];
double dis(double x1, double y1, double x2, double y2)
{
return sqrt(1.0 * (x1 - x2) * (x1 - x2) + 1.0 * (y1 - y2) * (y1 - y2));
}
bool cmp(node a, node b)
{
return a.value < b.value;
}
int find(int x)
{
return cun[x] == x ? x : cun[x] = find(cun[x]);
}
void shu(int x, int y)
{
int p = find(x);
int q = find(y);
if(p != q)
cun[p] = q;
}
int main()
{
int n;
double s[M], e[M];
while( cin >> n )
{
int temp = 1;
for(int i = 1; i <= n; ++i)
cin >> s[i] >> e[i];
for(int i = 1; i <= n; ++i)
for(int j = i + 1; j <= n; ++j)
{
a[temp].x = i;
a[temp].y = j;
a[temp].value = dis(s[i],e[i],s[j],e[j]);
temp++;
}
for(int i = 0; i <= n; ++i)
cun[i] = i;
sort(a,a + temp,cmp);
double ans = 0;
for(int i = 1; i < temp; ++i)
{
if(find(a[i].x) != find(a[i].y))
{
shu(a[i].x,a[i].y);
ans += a[i].value;
} } printf("%.2lf\n",ans);
}
return 0;
}