QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#621308#7883. Takeout DeliveringKdlyhWA 130ms38884kbC++202.3kb2024-10-08 12:35:062024-10-08 12:35:08

Judging History

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

  • [2024-10-08 12:35:08]
  • 评测
  • 测评结果:WA
  • 用时:130ms
  • 内存:38884kb
  • [2024-10-08 12:35:06]
  • 提交

answer

#include<bits/stdc++.h>

#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#define debug(...) 42
#endif

using i64 = long long;

namespace ranges = std::ranges;

struct DSU {
    std::vector<int> f, siz;
    
    DSU() {}
    DSU(int n) {
        init(n);
    }
    
    void init(int n) {
        f.resize(n);
        std::iota(f.begin(), f.end(), 0);
        siz.assign(n, 1);
    }
    
    int find(int x) {
        while (x != f[x]) {
            x = f[x] = f[f[x]];
        }
        return x;
    }
    
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    
    bool merge(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) {
            return false;
        }
        siz[x] += siz[y];
        f[y] = x;
        return true;
    }
    
    int size(int x) {
        return siz[find(x)];
    }
};


void solve()
{
    int n, m; std::cin >> n >> m; std::vector<std::tuple<int, int, int>> edges(m);//跟正常存边方式不一样,因为要对边排序
    for (int i = 0, u, v, w; i < m; i++) {std::cin >> u >> v >> w; --u; --v; edges[i] = {w, u, v};}
    ranges::sort(edges, std::greater()); DSU dsu(n); std::vector adj(n, std::vector<std::pair<int, int>>());
    for (const auto&[w, u, v] : edges) if (not dsu.same(u, v)) {dsu.merge(u, v); adj[u].emplace_back(v, w); adj[v].emplace_back(u, w);}

    std::array<std::vector<int>, 2> dist{std::vector<int>(n), std::vector<int>(n)};
    
    for (auto& cur : {0, 1}) {
        std::vector<int> vis(n);
        auto dfs = [&](auto& self, int now) -> void {
            vis[now] = true; for (auto&[to, w] : adj[now]) if (not vis[to]) {dist[cur][to] = std::max(dist[cur][now], w); self(self, to);}
        };
        dfs(dfs, cur ? n - 1 : 0);
    }

    int ans{std::numeric_limits<int>::max()}; for (const auto&[w, u, v] : edges) {
        for (auto& cur : {0, 1}) if (std::max(dist[cur][u], dist[cur ^ 1][v]) >= w) {ans = std::min(ans, std::max(dist[cur][u], dist[cur ^ 1][v]) + w);}
    }

    for (auto&[u, v] : {std::make_pair(0, n - 1), {n - 1, 0}}) for (auto&[to, w] : adj[u]) if (to == v) {ans = std::min(ans, w);}

    std::cout << ans << "\n";

}

signed main()
{
   std::cin.tie(nullptr)->sync_with_stdio(false);
   int _(1);
#ifdef tests
   std::cin >> _;
#endif
   while(_--) solve();
   return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 130ms
memory: 38884kb

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:

999998457

result:

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