代码求调

P3366 【模板】最小生成树

_6872_ @ 2022-08-31 10:15:47

Rt,63分。

8#9#10 RE,#13WA。

13可能是特判的问题,8、9、10显示数组越界,可是开大多少都没用,并且是在题目合理范围之内的。

#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 _6872_ @ 2022-08-31 10:16:01

标题行用错了/kk


by SamHJD @ 2022-08-31 10:21:28

代码没读懂,但是cnt可能超了5e5


by VectorLi @ 2022-08-31 10:27:16

为什么要加这个啊?

for (int i = 1; i <= n; i ++)
        for (int j = i + 1; j <= n; j ++) p = dist(i, j), add(i, j, p);

by Time_Limit_Exceed @ 2022-08-31 10:29:05

@6872 for (int i = 1; i <= n; i ++) for (int j = i + 1; j <= n; j ++) p = dist(i, j), add(i, j, p);去掉

再加个add(y,x,z)


by 032o35 @ 2022-08-31 10:29:42

楼上正解,改了之后90pts


by liangbowen @ 2022-08-31 10:29:51

看错题了吧?这和直线距离有啥关系


by Time_Limit_Exceed @ 2022-08-31 10:32:23

@6872 if (t!=n-1){ printf("orz\n"); return 0; }


by Time_Limit_Exceed @ 2022-08-31 10:32:38

这样就好了


by TimSwn090306 @ 2022-08-31 10:33:22

为神马要开double?


by _6872_ @ 2022-08-31 10:37:18

@TimSwn090306 去问贾老师()


| 下一页