結果

問題 No.1473 おでぶなおばけさん
ユーザー AnchorBluesAnchorBlues
提出日時 2023-03-08 13:54:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 184 ms / 2,000 ms
コード長 4,110 bytes
コンパイル時間 1,842 ms
コンパイル使用メモリ 181,416 KB
実行使用メモリ 11,688 KB
最終ジャッジ日時 2023-10-18 05:54:53
合計ジャッジ時間 9,309 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 128 ms
11,468 KB
testcase_03 AC 86 ms
10,428 KB
testcase_04 AC 71 ms
7,844 KB
testcase_05 AC 30 ms
5,036 KB
testcase_06 AC 129 ms
10,888 KB
testcase_07 AC 116 ms
11,684 KB
testcase_08 AC 184 ms
11,688 KB
testcase_09 AC 108 ms
11,684 KB
testcase_10 AC 37 ms
7,396 KB
testcase_11 AC 39 ms
9,084 KB
testcase_12 AC 38 ms
8,192 KB
testcase_13 AC 23 ms
6,428 KB
testcase_14 AC 17 ms
5,536 KB
testcase_15 AC 33 ms
7,784 KB
testcase_16 AC 34 ms
6,868 KB
testcase_17 AC 4 ms
4,348 KB
testcase_18 AC 6 ms
4,348 KB
testcase_19 AC 46 ms
7,728 KB
testcase_20 AC 68 ms
9,568 KB
testcase_21 AC 83 ms
9,304 KB
testcase_22 AC 73 ms
10,564 KB
testcase_23 AC 59 ms
9,612 KB
testcase_24 AC 59 ms
9,392 KB
testcase_25 AC 162 ms
10,452 KB
testcase_26 AC 163 ms
10,624 KB
testcase_27 AC 38 ms
6,124 KB
testcase_28 AC 145 ms
11,680 KB
testcase_29 AC 81 ms
9,308 KB
testcase_30 AC 109 ms
10,308 KB
testcase_31 AC 110 ms
11,508 KB
testcase_32 AC 103 ms
11,152 KB
testcase_33 AC 78 ms
9,568 KB
testcase_34 AC 33 ms
6,512 KB
testcase_35 AC 37 ms
6,928 KB
testcase_36 AC 66 ms
8,256 KB
testcase_37 AC 92 ms
9,252 KB
testcase_38 AC 14 ms
4,664 KB
testcase_39 AC 35 ms
6,868 KB
testcase_40 AC 34 ms
6,868 KB
testcase_41 AC 34 ms
6,564 KB
testcase_42 AC 34 ms
6,564 KB
testcase_43 AC 41 ms
8,968 KB
testcase_44 AC 41 ms
8,968 KB
testcase_45 AC 41 ms
8,968 KB
testcase_46 AC 61 ms
9,120 KB
testcase_47 AC 103 ms
10,380 KB
testcase_48 AC 107 ms
9,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<pll>>;

const ll INF = 1LL << 60;

template <class T>
void chmax(T& a, T b) {
    if (b > a) a = b;
}
template <class T>
void chmin(T& a, T b) {
    if (b < a) a = b;
}

template <typename T, typename S>
std::ostream& operator<<(std::ostream& os, const pair<T, S>& x) noexcept {
    return os << "(" << x.first << ", " << x.second << ")";
}

template <typename T>
void print_vector(vector<T> a) {
    cout << '[';
    for (int i = 0; i < a.size(); i++) {
        cout << a[i];
        if (i != a.size() - 1) {
            cout << ", ";
        }
    }
    cout << ']' << endl;
}

typedef ll idx;
typedef ll element;
typedef std::function<element(idx)> Query;

class BinarySearch {
   public:
    // コンストラクタ
    BinarySearch();
    // [left, right)の範囲で探索する
    BinarySearch(Query, idx left, idx right, bool ascending = true);

    // 要素xが存在するかどうかを判定し、存在したらそのインデックスを、存在しなかったら-1を返す
    idx find(element x) const;

    // 要素xの個数をカウントする
    ll count(element x) const;

    // ascending = true のとき: f(idx)>=xを満たす最小のidxを求める
    // ascending = false のとき: f(idx)<xを満たす最小のidxを求める
    // (いずれにしても、idxを小さい順から投げていったときにboolが反転する場所のidx)
    idx bound(element x) const;

   private:
    Query query_;
    idx left_;
    idx right_;
    bool ascending_;
};

// コンストラクタ
BinarySearch::BinarySearch() {}

BinarySearch::BinarySearch(Query query, idx left, idx right, bool ascending) {
    query_ = query;
    left_ = left;
    right_ = right;
    ascending_ = ascending;
}

idx BinarySearch::find(element x) const {
    idx left = left_;
    idx right = right_ - 1;

    while (right >= left) {
        idx mid = left + (right - left) / 2;
        element ret = query_(mid);
        if (ret == x)
            return mid;
        else if (!ascending_ ^ (ret > x))
            right = mid - 1;
        else
            left = mid + 1;
    }
    return -1;
}

ll BinarySearch::count(element x) const {
    idx a = bound(x);
    idx b = bound(x + 1);
    return std::abs(a - b);
}

idx BinarySearch::bound(element x) const {
    idx ng = left_ - 1;
    idx ok = right_;

    while (abs(ok - ng) > 1) {
        idx mid = (ok + ng) / 2;

        if (!ascending_ ^ (query_(mid) >= x))
            ok = mid;
        else
            ng = mid;
    }
    return ok;
}

vector<int> bfs(Graph& g, int start, ll weight) {
    ll N = g.size();
    queue<int> q;
    vector<int> dist(N, -1);
    vector<int> prevs(N, -1);
    dist[start] = 0;
    q.push(start);
    while (true) {
        if (q.empty()) break;
        auto current = q.front();
        q.pop();

        for (auto& v : g[current]) {
            if (weight > v.second) continue;
            if (dist[v.first] == -1) {
                dist[v.first] = dist[current] + 1;
                prevs[v.first] = current;
                q.push(v.first);
            }
        }
    }
    return prevs;
}

ll N, M;
Graph G;

ll f(ll weight) {
    auto prevs = bfs(G, 0, weight);
    if (prevs[N - 1] == -1) return false;
    return true;
}

int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    cin >> N >> M;
    G.resize(N);
    for (int i = 0; i < M; i++) {
        ll a, b, c;
        cin >> a >> b >> c;
        a--;
        b--;
        G[a].push_back({b, c});
        G[b].push_back({a, c});
    }
    auto bs = BinarySearch(f, 0, 1000000010, false);
    auto best_weight = bs.bound(1) - 1;
    auto prevs = bfs(G, 0, best_weight);
    ll current = N - 1;
    int cnt = 0;
    while (true) {
        if (current == 0) break;
        cnt++;
        current = prevs[current];
    }
    std::cout << best_weight << ' ' << cnt << "\n";
    return 0;
}
0