QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#274473#7883. Takeout DeliveringCSUST_GXL#WA 68ms22440kbC++201.8kb2023-12-03 15:52:272023-12-03 15:52:27

Judging History

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

  • [2023-12-03 15:52:27]
  • 评测
  • 测评结果:WA
  • 用时:68ms
  • 内存:22440kb
  • [2023-12-03 15:52:27]
  • 提交

answer

#include<bits/stdc++.h>

#define int long long
#define endl '\n'
using namespace std;
const int INF = 2e9;
const int N = 3e5 + 5;
bool p[N + 5], s[N + 5];
int pre[N + 5], suf[N + 5];

struct edge {
    int u, v, w;
};
vector<edge> G;

struct DSU {
    vector<int> pre, sz;

    DSU(int n) : pre(n + 1), sz(n + 1) {
        std::iota(pre.begin(), pre.end(), 0ll);
    }

    int find(int x) {
        return pre[x] == x ? x : pre[x] = find(pre[x]);
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    bool merge(int x, int y) {
        int fx = find(x), fy = find(y);
        if (fx == fy) return false;
        if (sz[fx] > sz[fy]) swap(fx, fy);
        sz[fy] += sz[fx];
        pre[fx] = fy;
        return true;
    }
};

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) pre[i] = suf[i] = INF, p[i] = s[i] = false;
    pre[1] = suf[n] = 0;
    p[1] = s[n] = true;
    for (int i = 1, u, v, w; i <= m; i++) {
        cin >> u >> v >> w;
        G.push_back({u, v, w});
    }
    std::sort(G.begin(), G.end(), [&](const edge a, const edge b) {
        return a.w < b.w;
    });
    int ans = INF;
    DSU dsu(n);
    for (auto [u, v, w]: G) {
        dsu.merge(u, v);
        if (!p[u] && dsu.same(1, u)) pre[u] = w, p[u] = true;
        if (!p[v] && dsu.same(1, v)) pre[v] = w, p[v] = true;
        if (!s[u] && dsu.same(n, u)) suf[u] = w, s[u] = true;
        if (!s[v] && dsu.same(n, v)) suf[v] = w, s[v] = true;
        int tmp = min(max(pre[u], suf[v]), max(pre[v], suf[u]));
        if (dsu.same(1, n)) ans = min(ans, tmp + w);
    }
    cout << ans << '\n';
}

signed main() {
    int t = 1;
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    while (t--) solve();
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 5488kb

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: 68ms
memory: 22440kb

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:

1999993064

result:

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