結果

問題 No.1690 Power Grid
ユーザー nebukuro09nebukuro09
提出日時 2021-09-24 22:22:19
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 877 ms / 3,000 ms
コード長 1,618 bytes
コンパイル時間 726 ms
コンパイル使用メモリ 104,664 KB
実行使用メモリ 84,036 KB
最終ジャッジ日時 2023-09-04 14:14:21
合計ジャッジ時間 13,235 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 592 ms
62,288 KB
testcase_07 AC 590 ms
63,296 KB
testcase_08 AC 588 ms
62,260 KB
testcase_09 AC 589 ms
63,772 KB
testcase_10 AC 404 ms
50,844 KB
testcase_11 AC 807 ms
80,496 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 65 ms
10,076 KB
testcase_15 AC 872 ms
81,512 KB
testcase_16 AC 610 ms
62,796 KB
testcase_17 AC 290 ms
31,848 KB
testcase_18 AC 132 ms
16,800 KB
testcase_19 AC 614 ms
62,288 KB
testcase_20 AC 628 ms
65,424 KB
testcase_21 AC 413 ms
46,688 KB
testcase_22 AC 722 ms
71,000 KB
testcase_23 AC 877 ms
84,036 KB
testcase_24 AC 796 ms
76,168 KB
testcase_25 AC 831 ms
76,816 KB
testcase_26 AC 847 ms
80,452 KB
testcase_27 AC 2 ms
4,376 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;

immutable long MOD = 998244353;
immutable long inf = 1L << 59;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto K = s[2];
    auto A = readln.split.map!(to!long).array;
    auto G = new long[][](N, N);
    foreach (i; 0..N) G[i][] = inf;
    foreach (i; 0..M) {
        s = readln.split.map!(to!int);
        auto u = s[0] - 1;
        auto v = s[1] - 1;
        auto z = s[2];
        G[u][v] = G[v][u] = z.to!long;
    }

    auto dist = new long[][](N, N);
    foreach (i; 0..N) foreach (j; 0..N) dist[i][j] = G[i][j];

    foreach (i; 0..N) foreach (j; 0..N) foreach (k; 0..N) if (dist[j][i] != inf && dist[i][k] != inf) {
        dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]);
    }

    auto dist2 = new long[][](N, 1<<N);
    foreach (i; 0..N) dist2[i][] = inf;
    foreach (i; 0..N) foreach (mask; 0..1<<N) {
        foreach (j; 0..N) if (mask & (1 << j)) dist2[i][mask] = min(dist2[i][mask], dist[i][j]);
    }

    auto dp = new long[][](K+1, 1<<N);
    foreach (i; 0..K+1) dp[i][] = inf;
    dp[0][0] = 0;

    foreach (i; 0..K) foreach (mask; 0..1<<N) foreach (j; 0..N) if (!(mask & (1 << j))) {
        if (i == 0) {
            dp[i+1][mask|(1<<j)] = min(dp[i+1][mask|(1<<j)], dp[i][mask] + A[j]);
        } else {
            dp[i+1][mask|(1<<j)] = min(dp[i+1][mask|(1<<j)], dp[i][mask] + A[j] + dist2[j][mask]);
        }
    }

    dp[K].reduce!min.writeln;
}
0