标签:答案 ace 有一个 tps https 频道 map 相同 位置
两两求距离,将最小生成树的每条边保存到数组,共p-1条边,s个卫星形成s-1条边。
较大s-1条边 使用微型,答案即a[p-s]。
#include <iostream> #include <memory.h> #include <string> #include <istream> #include <sstream> #include <vector> #include <stack> #include <algorithm> #include <map> #include <queue> #include <math.h> #include <cstdio> using namespace std; typedef long long LL; const int MAXM = 250000+10; const int MAXN = 500+10; struct Node { double _x,_y; }node[MAXN]; struct Path { int _l,_r; double _value; bool operator < (const Path & that)const{ return this->_value < that._value; } }path[MAXM]; int Father[MAXN]; double a[MAXN]; int n; int s,p; int Get_F(int x) { return Father[x] = (Father[x] == x) ? x : Get_F(Father[x]); } void Init() { for (int i = 1;i <= n;i++) Father[i] = i; } double Get_Len(Node a,Node b) { return sqrt((a._x - b._x) * (a._x - b._x) + (a._y - b._y) * (a._y - b._y)); } int main() { cin >> n; while (n--) { memset(a,0,sizeof(a)); cin >> s >> p; for (int i = 1;i <= p;i++) Father[i] = i; for (int i = 1;i <= p;i++) cin >> node[i]._x >> node[i]._y; int pos = 0; for (int i = 1;i <= p;i++) { for (int j = i + 1;j <= p;j++) { path[++pos]._l = i; path[pos]._r = j; path[pos]._value = Get_Len(node[i], node[j]); } } sort(path + 1,path + 1 + pos); int cnt = 0; for (int i = 1;i <= pos;i++) { int tl = Get_F(path[i]._l); int tr = Get_F(path[i]._r); if (tl != tr) { Father[tl] = tr; a[++cnt] = path[i]._value; } } printf("%.2lf\n",a[p-s]); } return 0; }
标签:答案 ace 有一个 tps https 频道 map 相同 位置
原文地址:https://www.cnblogs.com/YDDDD/p/10338933.html