File size: 1,742 Bytes
c4b0eef |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
/*
>>~~ UVa Online Judge ACM Problem Solution ~~<<
ID: 10034
Name: Freckles
Problem: https://onlinejudge.org/external/100/10034.pdf
Language: C++
Author: Arash Shakery
Email: [email protected]
*/
#include <math.h>
#include <stdio.h>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
double X[102], Y[102], M[102][102];
double dist(int i, int j) {
double x = X[i] - X[j],
y = Y[i] - Y[j];
return sqrt(x*x + y*y);
}
struct Edge {
int i, j;
double dist;
bool operator < (const Edge& o) const {
return dist < o.dist;
}
};
Edge edges[102*102];
bool tr[102][102];
int main(){
int N;
cin>>N;
while(N--) {
int n;
cin>>n;
for(int i=0; i<n; i++)
cin>>X[i]>>Y[i];
int l = 0;
for (int i=0; i<n; i++) {
M[i][i] = 0;
tr[i][i] = true;
for (int j=i+1; j<n; j++) {
Edge& e = edges[l++];
e.i=i; e.j=j;
e.dist = M[i][j] = M[j][i] = dist(i, j);
tr[i][j] = tr[j][i] = false;
}
}
sort(edges, edges+l);
int c = 0, k = 0;
double res = 0;
while (c < n-1) {
Edge& e = edges[k++];
if (!tr[e.i][e.j]) {
c++;
res += e.dist;
tr[e.i][e.j] = tr[e.j][e.i] = true;
for (int i=0; i<n; i++)
if (tr[i][e.i])
for (int j=0; j<n; j++)
if (tr[e.j][j])
tr[i][j] = tr[j][i] = true;
}
}
printf("%.2f\n", res);
if (N) cout<<endl;
}
}
|