当前位置: 代码迷 >> 综合 >> Uva - 10034 - Freckles
  详细解决方案

Uva - 10034 - Freckles

热度:76   发布时间:2024-01-10 13:21:47.0

题意:有n个斑点,用直线连起来使任意两点连通,求最短的画线距离和(0 < n <= 100)。

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21276

——>>白白净净的最小生成树。

另外发现了一个问题:每组数据(最后一组除外)后漏了空行,返回的是WA,不是CE!

#include <cstdio>
#include <cmath>
#include <queue>using namespace std;const int maxn = 100 + 10;
int n, fa[maxn];struct Point{double x;double y;
}p[maxn];double Dis(Point A, Point B){return sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y));
}struct node{int u;int v;double dis;bool operator < (const node& e) const{return dis > e.dis;}
};void read(){scanf("%d", &n);for(int i = 0; i < n; i++) scanf("%lf%lf", &p[i].x, &p[i].y);
}int Find(int x){return fa[x] == x ? x : fa[x] = Find(fa[x]);
}bool Union(int x, int y){int newx = Find(x);int newy = Find(y);if(newx == newy) return 0;fa[newy] = newx;return 1;
}void init(){for(int i = 0; i < n; i++) fa[i] = i;
}void Kruscal(){priority_queue<node> pq;for(int i = 0; i < n; i++)for(int j = i+1; j < n; j++)pq.push((node){i, j, Dis(p[i], p[j])});init();double ret = 0;while(!pq.empty()){node no = pq.top(); pq.pop();if(Union(no.u, no.v)) ret += no.dis;}printf("%.2f\n", ret);
}int main()
{int T;scanf("%d", &T);while(T--){read();Kruscal();if(T) puts("");}return 0;
}