結果

問題 No.417 チューリップバブル
ユーザー whesonwheson
提出日時 2018-05-17 23:46:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 316 ms / 2,000 ms
コード長 1,927 bytes
コンパイル時間 1,640 ms
コンパイル使用メモリ 173,860 KB
実行使用メモリ 5,708 KB
最終ジャッジ日時 2023-09-10 22:44:32
合計ジャッジ時間 6,859 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 9 ms
4,376 KB
testcase_10 AC 12 ms
4,380 KB
testcase_11 AC 47 ms
4,384 KB
testcase_12 AC 47 ms
4,388 KB
testcase_13 AC 18 ms
4,384 KB
testcase_14 AC 75 ms
4,380 KB
testcase_15 AC 7 ms
4,380 KB
testcase_16 AC 6 ms
4,380 KB
testcase_17 AC 40 ms
4,388 KB
testcase_18 AC 41 ms
4,380 KB
testcase_19 AC 41 ms
4,380 KB
testcase_20 AC 155 ms
4,408 KB
testcase_21 AC 153 ms
4,384 KB
testcase_22 AC 153 ms
4,384 KB
testcase_23 AC 153 ms
4,384 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 155 ms
4,504 KB
testcase_26 AC 15 ms
4,380 KB
testcase_27 AC 107 ms
4,384 KB
testcase_28 AC 153 ms
4,400 KB
testcase_29 AC 152 ms
4,480 KB
testcase_30 AC 153 ms
4,380 KB
testcase_31 AC 153 ms
4,384 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 5 ms
4,380 KB
testcase_34 AC 32 ms
4,380 KB
testcase_35 AC 312 ms
5,400 KB
testcase_36 AC 312 ms
5,404 KB
testcase_37 AC 313 ms
5,708 KB
testcase_38 AC 314 ms
5,620 KB
testcase_39 AC 316 ms
5,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define int long long

using namespace std;
using LL = long long;
using P = pair<int, int>;

#define FOR(i, a, n) for(int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)

#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()

const int INF = (int)1e9;
const LL INFL = (LL)1e15;
const int MOD = 1e9 + 7;

int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

template <typename T>
struct Edge{
    int to; T cost;
    Edge(int to, T cost) : to(to), cost(cost) {}
};
template <typename T>
using Edges = vector<Edge<T>>;
template <typename T>
using AdjList = vector<Edges<T>>;

/*************** using variables **************/
AdjList<int> adj;
int dp[205][2005]; // dp[i][j] := 頂点i以下の木に対して時間jをかけて得られる税収の最大値
int n, m;
vector<int> u;
/**********************************************/

// 深さ優先探索を使うことで葉から順にdpテーブルを埋めることができる
void dfs(int cur, int pre){
    dp[cur][0] = u[cur]; // 現在見ている頂点がcurであれば時間0をかけて得られるのはcurの税収のみ
    for(auto child: adj[cur]) if(child.to != pre){
        dfs(child.to, cur);
        
        // 深さ優先探索なのでdp[child.to][0 ~ m]は既に埋まっている

        for(int i = m; i >= 0; i--){
            for(int j = 0; j+child.cost*2 <= i; j++){
                dp[cur][i] = max(dp[cur][i], dp[cur][i-(j+child.cost*2)] + dp[child.to][j]);
            }
        }
    }
}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> n >> m;
    u.resize(n);
    REP(i, n) cin >> u[i];

    adj.resize(n);
    REP(i, n-1){
        int a, b, c;
        cin >> a >> b >> c;
        adj[a].pb(Edge<int>(b, c));
        adj[b].pb(Edge<int>(a, c));
    }
    
    dfs(0, -1);
    int ans = 0;
    REP(i, m+1) ans = max(ans, dp[0][i]);
    cout << ans << endl;
}
0