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 85 86
| #include <bits/stdc++.h> using namespace std; typedef double db; const int N = 110; const db inf = 1e18; typedef pair<db, int> P; int n, m;
struct Point { db x, y; };
struct Edge { int to; db w; Edge(int to, db w): to(to), w(w) {} }; Point p[N]; vector<Edge> G[N]; db d[N]; int v[N];
void init() { for(int i = 0; i < N; ++i) { G[i].clear(); } }
void add(int x, int y, db z) { G[x].push_back(Edge(y, z)); }
db prim(int s) { priority_queue<P, vector<P>, greater<P>> q; for(int i = 0; i <= n; ++i) { d[i] = inf; } memset(v, 0, sizeof(v)); db ans = 0; d[s] = 0; q.push(P(0, s)); while(q.size()) { P p = q.top(); q.pop(); int x = p.second; if(v[x]) continue; v[x] = 1; ans += d[x]; for(int i = 0; i < G[x].size(); ++i) { Edge e = G[x][i]; if (d[e.to] > e.w && !v[e.to]) { d[e.to] = e.w; q.push(P(d[e.to], e.to)); } } } return ans; }
db dis(Point a, Point b) { return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2)); }
int main() { int T; scanf("%d", &T); while(T--) { init(); scanf("%d", &n); for(int i = 1; i <= n; ++i) { scanf("%lf%lf", &p[i].x, &p[i].y); } for(int i = 1; i <= n; ++i) { for(int j = i + 1; j <= n; ++j) { db tmp = dis(p[i], p[j]); if(tmp >= 10 && tmp <= 1000) { add(i, j, tmp); add(j, i, tmp); } } } db ans = prim(1) * 100; if(ans == 0) printf("oh!\n"); else printf("%.1lf\n", ans); } return 0; }
|