結果

問題 No.1607 Kth Maximum Card
ユーザー nebukuro09nebukuro09
提出日時 2021-07-16 23:16:50
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 3,086 ms / 3,500 ms
コード長 2,038 bytes
コンパイル時間 991 ms
コンパイル使用メモリ 118,312 KB
実行使用メモリ 91,772 KB
最終ジャッジ日時 2023-09-04 13:18:43
合計ジャッジ時間 38,341 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 2,479 ms
91,772 KB
testcase_09 AC 1,951 ms
53,464 KB
testcase_10 AC 2,480 ms
88,868 KB
testcase_11 AC 514 ms
20,184 KB
testcase_12 AC 1,877 ms
53,496 KB
testcase_13 AC 361 ms
25,548 KB
testcase_14 AC 460 ms
25,460 KB
testcase_15 AC 2,144 ms
52,408 KB
testcase_16 AC 385 ms
25,484 KB
testcase_17 AC 113 ms
12,608 KB
testcase_18 AC 1,372 ms
50,424 KB
testcase_19 AC 812 ms
25,192 KB
testcase_20 AC 1,079 ms
49,896 KB
testcase_21 AC 1,192 ms
50,352 KB
testcase_22 AC 3,071 ms
89,072 KB
testcase_23 AC 3,086 ms
88,564 KB
testcase_24 AC 828 ms
29,924 KB
testcase_25 AC 503 ms
13,940 KB
testcase_26 AC 655 ms
22,784 KB
testcase_27 AC 919 ms
30,196 KB
testcase_28 AC 756 ms
25,300 KB
testcase_29 AC 1,604 ms
51,928 KB
testcase_30 AC 2,954 ms
53,140 KB
testcase_31 AC 1,531 ms
31,428 KB
testcase_32 AC 597 ms
38,576 KB
testcase_33 AC 601 ms
38,560 KB
testcase_34 AC 599 ms
38,612 KB
testcase_35 AC 596 ms
38,632 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 DList!(Edge!(T));
    pq.insertFront(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;
            if (e.cost == 0) {
                pq.insertFront(Edge!(T)(v, next_cost));
            } else {
                pq.insertBack(Edge!(T)(v, next_cost));
            }
        }
    }

    return dist;
}
0