QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#569019#7883. Takeout Deliveringheige#WA 545ms98960kbC++143.8kb2024-09-16 19:58:212024-09-16 19:58:22

Judging History

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

  • [2024-09-16 19:58:22]
  • 评测
  • 测评结果:WA
  • 用时:545ms
  • 内存:98960kb
  • [2024-09-16 19:58:21]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int kMaxN = 3e5 + 5, kMaxM = 1e6 + 5;
struct Edge {
    int to, w;
    Edge() {}
    Edge(int _to, int _w) : to(_to), w(_w) {}
    ~Edge() {}
};
struct Edgee {
    int u, v, w;
    Edgee() {}
    Edgee(int _u, int _v, int _w) : u(_u), v(_v), w(_w) {}
    ~Edgee() {}
    friend bool operator < (const Edgee& e1, const Edgee& e2) {return e1.w > e2.w;}
};
vector<vector<vector<Edge> > > G;
vector<vector<Edgee> > L;
int n, m, q, tot;
int lg[kMaxN], u[kMaxM], v[kMaxM], w[kMaxM], a[kMaxN], c[kMaxN];
int x, y;
long long ans = 1e18;
void addEdge(int x, int u, int v, int w) {G[x][u].emplace_back(Edge(v, w));}
namespace UFS {
int fa[kMaxN];
void init(int n) {for (int i = 1; i <= n; ++i) fa[i] = i;}
int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}
void unionn(int x, int y) {
    int fx = find(x), fy = find(y);
    if (fx != fy) fa[fx] = fy;
}
}
namespace Kruskal {
void kruskal(int x) {
    G[x].resize(n + 1); UFS::init(n);
    sort(L[x].begin(), L[x].end());
    for (auto ed : L[x]) {
        int u = ed.u, v = ed.v, w = ed.w;
        assert(w <= 0);
        if (UFS::find(u) == UFS::find(v)) continue ;
        UFS::unionn(u, v);
        addEdge(x, u, v, w); addEdge(x, v, u, w);
    }
}
}
namespace LCA {
int dep[kMaxN], fa[kMaxN][19], minn[kMaxN][19];
void prework(int x, int k, int fat, int ww) {
    dep[k] = dep[fat] + 1; minn[k][0] = ww; fa[k][0] = fat;
    for (int i = 1; (1 << i) <= dep[k]; ++i) {
        fa[k][i] = fa[fa[k][i - 1]][i - 1];
        minn[k][i] = min(minn[k][i - 1], minn[fa[k][i - 1]][i - 1]);
    }
    for (auto ed : G[x][k]) {
        int to = ed.to, w = ed.w;
        if (to == fat) continue ;
        prework(x, to, k, w);
    }
}
int LCA(int x, int y) {
    if (a[x] != a[y]) return 2e9;
    if (x == y) return 0;
    int ret = 1e9;
    if (dep[x] < dep[y]) swap(x, y);
    for (int i = lg[n]; ~i; --i) 
        if (dep[fa[x][i]] >= dep[y]) {ret = min(ret, minn[x][i]); x = fa[x][i];}
    if (x == y) return -ret;
    for (int i = lg[n]; ~i; --i)
        if (fa[x][i] != fa[y][i]) {ret = min(ret, min(minn[x][i], minn[y][i])); x = fa[x][i], y = fa[y][i];}
    ret = min(ret, min(minn[x][0], minn[y][0]));
    // cout << ret << endl;
    return -ret;
}
}
signed main() {
    cin >> n >> m;
    lg[0] = -1; UFS::init(n);
    for (int i = 1; i <= m; ++i) {
        cin >> u[i] >> v[i] >> w[i], w[i] = -w[i];
        UFS::unionn(u[i], v[i]);
    }
    for (int i = 1; i <= n; ++i) {
        lg[i] = lg[i >> 1] + 1;
        a[i] = c[i] = UFS::find(i);
    }
    sort(c + 1, c + 1 + n);
    tot = unique(c + 1, c + 1 + n) - (c + 1);
    for (int i = 1; i <= n; ++i) a[i] = lower_bound(c + 1, c + 1 + tot, a[i]) - c;
    L.resize(tot + 1), G.resize(tot + 1);
    for (int i = 1; i <= m; ++i) L[a[u[i]]].emplace_back(Edgee(u[i], v[i], w[i]));
    for (int i = 1; i <= tot; ++i) {
        Kruskal::kruskal(i);
        LCA::prework(i, 1, 0, 1e9);
    }
    // cout << LCA::LCA(1, 2) << endl;
    for (int i = 1; i <= m; i++) {
        int x = LCA::LCA(1, u[i]), y = LCA::LCA(v[i], n);
        ans = min(ans, max(-1ll * w[i] + 1ll * max(x, y), 1ll * (x + y)));
        x = LCA::LCA(1, v[i]), y = LCA::LCA(u[i], n);
        ans = min(ans, max(-1ll * w[i] + 1ll * max(x, y), 1ll * (x + y)));
        // ans = min(ans, -1ll * w[i] + 1ll * max(LCA::LCA(1, u[i]), LCA::LCA(v[i], n)));
        // ans = min(ans, -1ll * w[i] + 1ll * max(LCA::LCA(1, v[i]), LCA::LCA(u[i], n)));
        // cout << u[i] << ' ' << v[i] << ' ' << -w[i] << ' ' << LCA::LCA(1, u[i]) << ' ' << LCA::LCA(v[i], n) << endl;
        // cout << v[i] << ' ' << u[i] << ' ' << -w[i] << ' ' << LCA::LCA(1, v[i]) << ' ' << LCA::LCA(u[i], n) << endl;
    }
    cout << ans << '\n';
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 17924kb

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
Wrong Answer
time: 545ms
memory: 98960kb

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:

1356916107

result:

wrong answer 1st numbers differ - expected: '1999991697', found: '1356916107'