_6872_ @ 2022-08-31 10:15:47
Rt,63分。
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5+10;
long long ans = 0, p, z;
struct IAKIOI{ int x, y; } a[N];
int n, m, x, y;
int t, cnt = 0;
int f[N];
struct EDGE{
int from, to;
double w;
} e[N];
inline int getfa(int x){
if (f[x] == x) return x;
return f[x] = getfa(f[x]);
}
double dist(int x, int y){
return (double)sqrt((double)(a[x].x - a[y].x) *
(a[x].x - a[y].x) +
(double)(a[x].y - a[y].y) *
(a[x].y - a[y].y));
}
void add(int i, int j, double k){ e[++ cnt].from = i, e[cnt].to = j, e[cnt].w = k; }
bool cmp(EDGE a, EDGE b){
if (a.w == b.w) return a.from < b.from;
return a.w < b.w;
}
int main(){
scanf("%d %d", &n, &m);
if (n == 1){
printf("orz\n");
return 0;
}
for (int i = 1; i <= n; i ++) f[i] = i;
for (int i = 1; i <= m; i ++) scanf("%d %d %d", &x, &y, &z), add(x, y, z);
sort(e + 1, e + cnt + 1, cmp);
for (int i = 1; i <= n; i ++)
for (int j = i + 1; j <= n; j ++) p = dist(i, j), add(i, j, p);
for (int i = 1; i <= cnt; i ++){
x = getfa(e[i].from), y = getfa(e[i].to);
if (x != y) f[x] = y, t ++, ans += e[i].w;
if (t + 1 == n) break;
}
if (!ans){
printf("orz\n");
return 0;
}
printf("%d\n", ans);
return 0;
}
qwq
by Siegerkranz_2735 @ 2022-08-31 10:43:45
@6872 这个不考
by Dream_weavers @ 2022-08-31 10:44:38
为啥开double
by Acerkaio @ 2022-08-31 12:24:47
@6872 dist() 是?
by TimSwn090306 @ 2022-08-31 18:35:59
@6872 你这也不能照抄另一道题(P2872)吧? 不同点:本题是已给边,另一题是自行连边,所以你的dist和double类型都用不着
by _6872_ @ 2022-08-31 19:21:40
@TimSwn090306 IEE,翻了一下,好像你说的真没错……