求调,难道不是对中之对?
查看原帖
求调,难道不是对中之对?
22716
神仙酱楼主2024/3/14 23:10
#include <bits/stdc++.h>
#include <cstdio>
using namespace std;
const int N = 2e5 + 10;
long long d1[N], c, t[N], d2[N];
int e[N << 1], nxt[N << 1], idx, h[N];
int n, m, q, len;
inline int read() {
    int x = 0, flag = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-')
            flag = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + (ch ^ 48);
        ch = getchar();
    }
    return x * flag;
}
void add(int a, int b, int w) {
    e[idx] = b, t[idx] = w, nxt[idx] = h[a], h[a] = idx++;
}

void dfs(int u, int fa) {
    for (int i = h[u]; ~i; i = nxt[i]) {
        int son = e[i];
        if (son == fa)
            continue;
        d1[son] = d1[u] + t[i];
        if (d1[son] > d1[c])
            c = son;
        dfs(son, u);
    }
}

void dfs2(int u, int fa) {
    for (int i = h[u]; ~i; i = nxt[i]) {
        int son = e[i];
        if (son == fa)
            continue;
        d2[son] = d2[u] + t[i];
        dfs2(son, u);
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen(".in", "r", stdin);
    // freopen(".out", "w", stdout);
#endif
    n = read(), m = read();
    memset(h, -1, sizeof(h));

    for (int i = 1; i <= m; ++i) {
        int x = read(), y = read(), w = read();
        add(x, y, w);
        add(y, x, w);
    }

    dfs(1, -1);
    d1[c] = 0;
    dfs(c, -1);
    long long d = d1[c];
    dfs2(c, -1);
    long long res = 0;
    for (int i = 1; i <= n; i++)
        res = max(res, d + min(d1[i], d2[i]));
    printf("%lld\n", res);
    return 0;
}
2024/3/14 23:10
加载中...