結果

問題 No.1607 Kth Maximum Card
ユーザー nebukuro09nebukuro09
提出日時 2021-07-16 23:13:49
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 3,211 ms / 3,500 ms
コード長 1,935 bytes
コンパイル時間 1,024 ms
コンパイル使用メモリ 129,956 KB
実行使用メモリ 97,216 KB
最終ジャッジ日時 2023-09-04 13:17:41
合計ジャッジ時間 42,456 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 3,132 ms
54,436 KB
testcase_09 AC 2,487 ms
54,856 KB
testcase_10 AC 3,107 ms
55,104 KB
testcase_11 AC 637 ms
14,956 KB
testcase_12 AC 2,386 ms
55,008 KB
testcase_13 AC 409 ms
17,796 KB
testcase_14 AC 510 ms
22,184 KB
testcase_15 AC 2,446 ms
74,912 KB
testcase_16 AC 428 ms
24,744 KB
testcase_17 AC 128 ms
13,432 KB
testcase_18 AC 1,535 ms
54,136 KB
testcase_19 AC 945 ms
22,404 KB
testcase_20 AC 1,253 ms
53,684 KB
testcase_21 AC 1,372 ms
53,348 KB
testcase_22 AC 2,945 ms
54,984 KB
testcase_23 AC 2,988 ms
54,128 KB
testcase_24 AC 859 ms
37,200 KB
testcase_25 AC 546 ms
20,876 KB
testcase_26 AC 680 ms
14,736 KB
testcase_27 AC 956 ms
38,284 KB
testcase_28 AC 774 ms
37,780 KB
testcase_29 AC 1,746 ms
56,324 KB
testcase_30 AC 3,211 ms
97,216 KB
testcase_31 AC 1,661 ms
34,332 KB
testcase_32 AC 603 ms
38,924 KB
testcase_33 AC 606 ms
38,908 KB
testcase_34 AC 604 ms
38,928 KB
testcase_35 AC 600 ms
38,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdlib, std.datetime;


void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto K = s[2];
    auto G = new Tuple!(int, int)[][](N);
    foreach (i; 0..M) {
        s = readln.split.map!(to!int);
        auto u = s[0] - 1;
        auto v = s[1] - 1;
        auto c = s[2];
        G[u] ~= tuple(v, c);
        G[v] ~= tuple(u, c);
    }

    auto hi = 2 * 10 ^^5;
    auto lo = -1;

    while (hi - lo > 1) {
        auto mid = (hi + lo) / 2;
        if (search(N, M, K, G, mid)) {
            hi = mid;
        } else {
            lo = mid;
        }
    }

    hi.writeln;
}

bool search(int N, int M, int K, Tuple!(int, int)[][] G, int mid) {
    auto H = new Edge!(int)[][](N);
    foreach (i; 0..N) foreach (t; G[i]) {
        auto j = t[0];
        auto c = t[1];
        H[i] ~= Edge!(int)(j, (c <= mid ? 0 : 1));
    }

    auto d = dijkstra!(int, 1 << 29)(H, 0)[N-1];
    return d < K;
}

struct Edge(T) {
    int to;
    T cost;
}

T[] dijkstra(T, T inf)(const Edge!(T)[][] graph, int start) {
    import std.typecons : Tuple, tuple;
    import std.conv : to;
    import std.container : BinaryHeap;

    int n = graph.length.to!int;
    auto dist = new T[](n);
    dist[] = inf;
    dist[start] = 0;

    auto pq = new BinaryHeap!(Array!(Edge!(T)), "a.cost > b.cost");
    pq.insert(Edge!(T)(start, 0));

    while (!pq.empty) {
        auto u = pq.front.to;
        auto cost = pq.front.cost;
        pq.removeFront;
        foreach (const e; graph[u]) {
            auto v = e.to;
            auto next_cost = cost + e.cost;
            if (dist[v] <= next_cost) continue;
            dist[v] = next_cost;
            pq.insert(Edge!(T)(v, next_cost));
        }
    }

    return dist;
}
0