結果

問題 No.357 品物の並び替え (Middle)
ユーザー shi-moshi-mo
提出日時 2016-04-01 22:39:01
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 943 bytes
コンパイル時間 1,290 ms
コンパイル使用メモリ 142,212 KB
実行使用メモリ 22,468 KB
最終ジャッジ日時 2023-09-02 21:00:21
合計ジャッジ時間 3,160 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,372 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 2 ms
4,368 KB
testcase_04 AC 3 ms
4,368 KB
testcase_05 AC 3 ms
4,368 KB
testcase_06 AC 2 ms
4,372 KB
testcase_07 AC 1 ms
4,368 KB
testcase_08 AC 5 ms
4,368 KB
testcase_09 AC 93 ms
11,736 KB
testcase_10 AC 19 ms
4,536 KB
testcase_11 AC 19 ms
4,488 KB
testcase_12 AC 209 ms
22,468 KB
testcase_13 WA -
testcase_14 AC 93 ms
11,796 KB
testcase_15 AC 19 ms
4,480 KB
testcase_16 AC 42 ms
7,908 KB
testcase_17 AC 20 ms
4,552 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