結果

問題 No.417 チューリップバブル
ユーザー izuru_matsuuraizuru_matsuura
提出日時 2016-12-20 18:31:49
言語 D
(dmd 2.105.2)
結果
WA  
実行時間 -
コード長 2,021 bytes
コンパイル時間 1,908 ms
コンパイル使用メモリ 146,588 KB
実行使用メモリ 6,844 KB
最終ジャッジ日時 2023-09-03 00:03:39
合計ジャッジ時間 8,085 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 WA -
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 4 ms
4,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 617 ms
6,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void log(A...)(A arg) { stderr.writeln(arg); }
int size(T)(in T s) { return cast(int)s.length; }

struct Edge {
    int from, to, cost;
}

void main() {
    int N, M; readf("%s %s\n", &N, &M);
    auto U = new int[N];
    foreach (i; 0 .. N) {
        U[i] = readln.chomp.to!int;
    }
    auto G = new Edge[][N];
    auto D = new int[][](N, N);
    foreach (i; 0 .. N) foreach (j; 0 .. N) D[i][j] = int.max / 4;
    foreach (i; 0 .. N - 1) {
        int s, t, c; readf("%s %s %s\n", &s, &t, &c);
        G[s] ~= Edge(s, t, c);
        G[t] ~= Edge(t, s, c);
        D[s][t] = D[t][s] = c;
    }

    auto Vi = new int[N];
    auto Vp = new int[N];
    void dfs(int p, int v, ref int i) {
        //log([p, v, i]);
        Vi[i] = v;
        Vp[i] = p;
        i++;
        foreach (e; G[v]) {
            if (e.to == p) continue;
            dfs(v, e.to, i);
        }
    }
    int _ = 0; dfs(-1, 0, _);
    //log("Vi: ", Vi);
    //log("Vp: ", Vp);

    auto dp = new int[][][](2, N + 1, M + 1);
    foreach (j; 0 .. M + 1) dp[0][0][j] = U[0];
    for (int i = 1; i < N; i++) {
        for (int t = 0; t <= M; t++) {
            int p = Vp[i];
            int d = D[Vi[i]][p];
            dp[i % 2][Vi[i]][t] = max(dp[i % 2][Vi[i]][t], dp[(i - 1) % 2][Vi[i]][t]);
            if (t - 2 * d >= 0) dp[i % 2][Vi[i]][t] = dp[(i - 1) % 2][p][t - 2 * d];
            for (int j = 0; j < i; j++) {
                dp[i % 2][Vi[j]][t] = max(dp[i % 2][Vi[j]][t], dp[(i - 1) % 2][Vi[j]][t]);
                if (t - 1 >= 0) dp[i % 2][Vi[j]][t] = max(dp[i % 2][Vi[j]][t], dp[i % 2][Vi[j]][t - 1]);
                if (t - 2 * d >= 0) 
                    dp[i % 2][Vi[j]][t] = max(dp[i % 2][Vi[j]][t], dp[(i - 1) % 2][Vi[j]][t - 2 * d] + U[Vi[i]]);
            }
        }
    }
    writeln(dp[(N - 1) % 2][0][M]);


}
0