結果

問題 No.357 品物の並び替え (Middle)
ユーザー shi-moshi-mo
提出日時 2016-04-02 01:40:25
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 246 ms / 5,000 ms
コード長 948 bytes
コンパイル時間 1,322 ms
コンパイル使用メモリ 142,444 KB
実行使用メモリ 22,336 KB
最終ジャッジ日時 2023-09-02 21:00:32
合計ジャッジ時間 3,163 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,372 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 2 ms
4,372 KB
testcase_04 AC 3 ms
4,372 KB
testcase_05 AC 3 ms
4,372 KB
testcase_06 AC 2 ms
4,368 KB
testcase_07 AC 1 ms
4,368 KB
testcase_08 AC 5 ms
4,372 KB
testcase_09 AC 106 ms
12,016 KB
testcase_10 AC 21 ms
4,548 KB
testcase_11 AC 21 ms
4,548 KB
testcase_12 AC 246 ms
22,336 KB
testcase_13 AC 107 ms
11,748 KB
testcase_14 AC 107 ms
11,708 KB
testcase_15 AC 21 ms
4,492 KB
testcase_16 AC 47 ms
8,120 KB
testcase_17 AC 22 ms
4,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio;
import std.array;
import std.conv;
import std.algorithm;

uint[string] dp;

void main() {
    uint n, m; readf("%s %s\n", &n, &m);
    uint[][] score = loadScoreTable(n, m);

    uint[] a; foreach (i; 0..n) a ~= i;
    writeln(dfs([], a, score));
}

uint[][] loadScoreTable(uint n, uint m) {
    uint[][] table = new uint[][](n, n);

    foreach (_; 0..m) {
        uint i, j, s;
        readf("%s %s %s\n", &i, &j, &s);
        table[i][j] = s;
    }
    return table;
}

uint dfs(uint[] done, const uint[] left, const uint[][] score) {
    sort(done);
    string key = done.map!(to!string).join("-");

    if (key in dp) return dp[key];
    if (0 == left.length) return 0;

    uint maxScore = 0;
    foreach (i; left) {
        uint s = 0;
        foreach (j; done) s += score[i][j];
        s += dfs(done ~ i, left.dup.remove!(a => i == a), score);

        maxScore = max(maxScore, s);
    }
    return dp[key] = maxScore;
}
0