QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#621313#7883. Takeout DeliveringKdlyhWA 1ms3644kbC++202.3kb2024-10-08 12:43:042024-10-08 12:43:04

Judging History

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

  • [2024-10-08 12:43:04]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3644kb
  • [2024-10-08 12:43:04]
  • 提交

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); 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;
}

详细

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3644kb

input:

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

output:

4

result:

wrong answer 1st numbers differ - expected: '5', found: '4'