[USACO14MAR]Watering the Fields S - 洛谷https://www.luogu.com.cn/problem/P2212
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <unordered_map>
#include <cmath>
#include <map>
#include <cctype>
#include <cstdlib>
#include <deque>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1000010;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false)
#define lowbit(x) ((x)&(-x))
int n, c;
struct node {
int x, y;
};
node ver[MAXN];
bool vis[MAXN];
inline int dis(int a, int b) {
return (ver[a].x - ver[b].x) * (ver[a].x - ver[b].x) + (ver[a].y - ver[b].y) * (ver[a].y - ver[b].y);
}
int d[MAXN];
int main() {
scanf("%d %d", &n, &c);
for (int i = 1; i <= n; i++) {
scanf("%d %d", &ver[i].x, &ver[i].y);
}
for (int i = 1; i <= n; i++) {
d[i] = INF;
}
int ans = 0;
int pos = 1, now = 1;
while (true) {
vis[pos] = true;
for (int i = 1; i <= n; i++) {
int t = dis(pos, i);
if (t >= c && t < d[i]) {
d[i] = t;
}
}
int minn = INF;
for (int i = 1; i <= n; i++) {
if (!vis[i] && minn > d[i]) {
minn = d[i], pos = i;
}
}
if (minn == INF)
break;
ans += minn, now++;
//printf("%d\n", minn);
}
if (now != n) {
printf("-1");
} else {
printf("%d", ans);
}
return 0;
}