結果

問題 No.417 チューリップバブル
ユーザー Eliza_0xEliza_0x
提出日時 2018-05-19 17:30:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 950 ms / 2,000 ms
コード長 1,259 bytes
コンパイル時間 825 ms
コンパイル使用メモリ 90,064 KB
実行使用メモリ 4,856 KB
最終ジャッジ日時 2023-09-10 23:46:00
合計ジャッジ時間 13,541 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 13 ms
4,376 KB
testcase_09 AC 26 ms
4,380 KB
testcase_10 AC 37 ms
4,380 KB
testcase_11 AC 146 ms
4,380 KB
testcase_12 AC 146 ms
4,376 KB
testcase_13 AC 57 ms
4,380 KB
testcase_14 AC 231 ms
4,380 KB
testcase_15 AC 16 ms
4,380 KB
testcase_16 AC 16 ms
4,380 KB
testcase_17 AC 120 ms
4,376 KB
testcase_18 AC 119 ms
4,380 KB
testcase_19 AC 120 ms
4,376 KB
testcase_20 AC 472 ms
4,376 KB
testcase_21 AC 470 ms
4,380 KB
testcase_22 AC 469 ms
4,376 KB
testcase_23 AC 471 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 471 ms
4,380 KB
testcase_26 AC 42 ms
4,380 KB
testcase_27 AC 327 ms
4,380 KB
testcase_28 AC 470 ms
4,384 KB
testcase_29 AC 470 ms
4,380 KB
testcase_30 AC 470 ms
4,380 KB
testcase_31 AC 469 ms
4,376 KB
testcase_32 AC 6 ms
4,376 KB
testcase_33 AC 18 ms
4,380 KB
testcase_34 AC 208 ms
4,376 KB
testcase_35 AC 949 ms
4,560 KB
testcase_36 AC 946 ms
4,696 KB
testcase_37 AC 950 ms
4,664 KB
testcase_38 AC 950 ms
4,856 KB
testcase_39 AC 950 ms
4,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std; 

struct Edge {
    int to;
    int cost;
    Edge(int to, int cost): to(to), cost(cost) {}
};

int main() {
    int n, m; cin >> n >> m;
    vector<int> points(n);
    for(auto &&x: points) cin >> x;
    vector<vector<Edge>> edges(n);
    for (int i=0; i<n-1; i++) {
        int from, to, cost; cin >> from >> to >> cost;
        edges[from].push_back(Edge(to, cost));
        edges[to].push_back(Edge(from, cost));
    }
    vector<vector<int>> memo(n, vector<int>(m+1, 0));

    function<void(int, int)> dfs = [&](int cur, int prev) -> void {
        for (int i=0; i<m+1; i++) memo[cur][i] = points[cur];
        for (auto edge: edges[cur]) if (edge.to != prev) {
            dfs(edge.to, cur);
            for (int i=m; i>=0; i--) {
                for (int j=0; j<=i+edge.cost*2; j++) {
                    if (i-(j+edge.cost*2) >= 0)
                        memo[cur][i] = max(
                            memo[cur][i],
                            memo[cur][i-(j+edge.cost*2)] + memo[edge.to][j]);
                }
            }
        }
    };

    dfs(0, -1);
    cout << *max_element(memo[0].begin(), memo[0].end()) << endl;
}
0