結果

問題 No.1473 おでぶなおばけさん
ユーザー cutmdocutmdo
提出日時 2024-07-19 00:06:30
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,568 bytes
コンパイル時間 1,209 ms
コンパイル使用メモリ 109,904 KB
実行使用メモリ 21,308 KB
最終ジャッジ日時 2024-07-19 00:06:49
合計ジャッジ時間 15,574 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 515 ms
20,228 KB
testcase_03 AC 372 ms
17,448 KB
testcase_04 AC 351 ms
12,740 KB
testcase_05 AC 97 ms
6,524 KB
testcase_06 AC 425 ms
19,780 KB
testcase_07 AC 618 ms
21,124 KB
testcase_08 AC 660 ms
21,296 KB
testcase_09 AC 522 ms
21,308 KB
testcase_10 AC 120 ms
12,032 KB
testcase_11 AC 121 ms
14,208 KB
testcase_12 AC 123 ms
13,312 KB
testcase_13 AC 73 ms
9,216 KB
testcase_14 AC 57 ms
7,552 KB
testcase_15 AC 100 ms
11,776 KB
testcase_16 AC 112 ms
11,776 KB
testcase_17 AC 10 ms
5,376 KB
testcase_18 AC 15 ms
5,376 KB
testcase_19 AC 134 ms
10,688 KB
testcase_20 AC 246 ms
15,948 KB
testcase_21 AC 222 ms
15,120 KB
testcase_22 AC 211 ms
18,512 KB
testcase_23 AC 198 ms
16,672 KB
testcase_24 AC 175 ms
16,316 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 413 ms
21,056 KB
testcase_29 AC 275 ms
16,324 KB
testcase_30 AC 377 ms
18,452 KB
testcase_31 AC 388 ms
20,736 KB
testcase_32 AC 317 ms
18,500 KB
testcase_33 AC 263 ms
15,668 KB
testcase_34 AC 145 ms
10,008 KB
testcase_35 AC 122 ms
9,488 KB
testcase_36 AC 212 ms
14,404 KB
testcase_37 AC 339 ms
15,796 KB
testcase_38 AC 51 ms
5,936 KB
testcase_39 AC 111 ms
11,904 KB
testcase_40 AC 110 ms
11,904 KB
testcase_41 AC 97 ms
12,112 KB
testcase_42 AC 97 ms
12,116 KB
testcase_43 AC 211 ms
16,940 KB
testcase_44 AC 213 ms
16,936 KB
testcase_45 AC 211 ms
16,940 KB
testcase_46 AC 366 ms
15,820 KB
testcase_47 AC 493 ms
18,656 KB
testcase_48 AC 330 ms
17,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define PROBLEM "https://yukicoder.me/problems/no/1473"

#include <iostream>
#include <vector>
#include <queue>
#include <vector>
#include <deque>
#include <tuple>

template<class Node = int, class Cost = long long>
class Graph {
    //using Node = int;
    //using Cost = long long;

    using Edge = std::pair<Node, Cost>;
    using Edges = std::vector<Edge>;

    const int m_n;
    std::vector<Edges> m_graph;

public:
    Graph(int n) :m_n(n), m_graph(n) {}

    auto addEdge(const Node& f, const Node& t, const Cost& c = 1) {
        m_graph[f].emplace_back(t, c);
    }
    auto addEdgeUndirected(const Node& f, const Node& t, const Cost& c = 1) {
        addEdge(f, t, c); addEdge(t, f, c);
    }
    auto getEdges(const Node& from)const {
        class EdgesRange {
            const typename Edges::const_iterator b, e;
        public:
            EdgesRange(const Edges& edges) :b(edges.begin()), e(edges.end()) {}
            auto begin()const { return b; }
            auto end()const { return e; }
        };
        return EdgesRange(m_graph[from]);
    }
    auto getEdges()const {
        std::deque<std::tuple<Node, Node, Cost>> edges;
        for(Node from = 0; from < m_n; ++from) for(const auto& [to, c] : getEdges(from)) {
            edges.emplace_back(from, to, c);
        }
        return edges;
    }
    auto getEdgesExcludeCost()const {
        std::deque<std::pair<Node, Node>> edges;
        for(Node from = 0; from < m_n; ++from) for(const auto& [to, _] : getEdges(from)) {
            edges.emplace_back(from, to);
        }
        return edges;
    }
    auto reverse()const {
        auto rev = Graph<Node, Cost>(m_n);
        for(const auto& [from, to, c] : getEdges()) {
            rev.addEdge(to, from, c);
        }
        return rev;
    }
    auto size()const { return m_n; };
    auto debug(bool directed = false)const {
        for(const auto& [f, t, c] : getEdges())if(f < t || directed) {
            std::cout << f << " -> " << t << ": " << c << std::endl;
        }
    }
};
template <class Lambda>
auto binarySearch(long long ok, long long ng, const Lambda& is_ok) {
    while(std::abs(ok - ng) > 1) {
        long long mid = (ok + ng) >> 1;
        (is_ok(mid) ? ok : ng) = mid;
    }
    return ok;
}
template<class Node, class Cost, class Lambda>
auto bfs(const Graph<Node, Cost>& graph, const Node& root, const Lambda& lambda) {
    auto n = graph.size();
    std::vector<bool> used(n); used[root] = true;
    std::queue<Node> q; q.emplace(root);
    while(!q.empty()) {
        auto from = q.front();
        q.pop();
        for(const auto& [to, _] : graph.getEdges(from)) {
            if(used[to]) { continue; }
            q.emplace(to);
            used[to] = true;
            lambda(from, to);
        }
    }
}

using ll = long long;
using std::cout;
using std::cin;
constexpr char endl = '\n';

signed main() {
    ll n, m;
    cin >> n >> m;
    auto graph_all = Graph(n);
    for(int i = 0; i < m; ++i) {
        ll s, t, d;
        cin >> s >> t >> d;
        graph_all.addEdgeUndirected(s - 1, t - 1, d);
    }

    auto solve = [&](ll w) {
        auto graph = Graph(n);
        for(const auto& [s, t, d] : graph_all.getEdges()) {
            if(w <= d) { graph.addEdge(s, t); }
        }

        std::vector<int> dv(n);
        bfs(graph, 0, [&](ll f, ll t) {dv[t] = dv[f] + 1; });
        return dv[n - 1];
    };

    auto w_max = binarySearch(0, 1e9, [&](ll w) {
        auto d = solve(w);
        return d > 0;
    });

    auto ans = solve(w_max);
    cout << w_max << " " << ans << endl;
}
0