QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#770795#7883. Takeout DeliveringfosovML 4ms82220kbC++142.3kb2024-11-22 00:15:532024-11-22 00:15:59

Judging History

你现在查看的是最新测评结果

  • [2024-11-22 00:15:59]
  • 评测
  • 测评结果:ML
  • 用时:4ms
  • 内存:82220kb
  • [2024-11-22 00:15:53]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define ll long long 
#define lll __int128
#define INF 0x3f3f3f3f
#define LNF 0x3f3f3f3f3f3f3f3fll
#define MOD 998244353
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ld long double
#define fi first
#define se second
#define all(a) a.begin(), a.end()

#define N 300010
#define K 22

vector<int> G[N<<2];
int w[N<<2], dep[N<<2], f[K][N<<2];

void add_edge(int u, int v) {
    G[u].emplace_back(v);
    G[v].emplace_back(u);
}
void dfs(int u, int fa, int d) {
    f[0][u] = fa;
    dep[u] = d;
    for (auto& v : G[u]) {
        if (v == fa) continue;
        dfs(v, u, d+1);
    }
}
void st(int n) {
    for (int i = 1; i < K; ++ i) {
        for (int j = 1; j <= n; ++ j) {
            f[i][j] = f[i-1][f[i-1][j]];
        }
    }
}
int lca(int u, int v) {
    if (dep[u] < dep[v]) swap(u, v);
    int d = dep[u] - dep[v];
    for (int i = 0; d; d>>=1, ++i) if (d&1) u = f[i][u];
    for (int i = K-1; i >= 0; -- i) {
        if (f[i][u] != f[i][v]) {
            u = f[i][u], v = f[i][v];
        }
    }
    return u == v ? u : f[0][u];
}

int p[N], rt[N], z, flg[N];
void init(int n) {
    for (int i = 1; i <= n; ++ i) p[i] = rt[i] = i;
    z = n;
}
int find(int x) {
    return p[x] == x ? x : (p[x] = find(p[x]));
}
void join(int u, int v, int l) {
    u = find(u), v = find(v);
    if (u == v) return;
    w[++ z] = l;
    add_edge(z, rt[u]), add_edge(z, rt[v]);
    p[u] = v;
    rt[v] = z;
}

int main() {
#ifdef TEST
    freopen("zz.in", "r+", stdin);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n, m; cin >> n >> m;
    init(n);
    vector<tuple<int, int, int>> edgs;
    for (int i = 0; i < m; ++ i) {
        int u, v, l; cin >> u >> v >> l;
        edgs.emplace_back(l, u, v);
    }
    int res = INF;
    sort(edgs.begin(), edgs.end());
    for (int i = 0; i < m; ++ i) {
        auto& [l, u, v] = edgs[i];
        join(u, v, l);
        flg[i] = find(1) == find(n);
    }
    dfs(z, 0, 0);
    st(z);

    for (int i = 0; i < m; ++ i) {
        if (!flg[i]) continue;
        auto& [l, u, v] = edgs[i];
        res = min(res, l + max(w[lca(u, 1)], w[lca(v, n)]));
        res = min(res, l + max(w[lca(u, n)], w[lca(v, 1)]));
    }

    cout << res << '\n';
} 


詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 82220kb

input:

4 6
1 2 2
1 3 4
1 4 7
2 3 1
2 4 3
3 4 9

output:

5

result:

ok 1 number(s): "5"

Test #2:

score: -100
Memory Limit Exceeded

input:

300000 299999
80516 80517 597830404
110190 110191 82173886
218008 218009 954561262
250110 250111 942489774
66540 66541 156425292
34947 34948 239499776
273789 273790 453201232
84428 84429 439418398
98599 98600 326095035
55636 55637 355015760
158611 158612 684292473
43331 43332 43265001
171621 171622 ...

output:

1061109567

result: