結果

問題 No.417 チューリップバブル
ユーザー ShibuyapShibuyap
提出日時 2021-02-05 04:47:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 8 ms / 2,000 ms
コード長 2,131 bytes
コンパイル時間 2,214 ms
コンパイル使用メモリ 208,728 KB
実行使用メモリ 13,148 KB
最終ジャッジ日時 2023-09-14 03:00:28
合計ジャッジ時間 4,329 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
12,856 KB
testcase_01 AC 6 ms
12,888 KB
testcase_02 AC 6 ms
12,852 KB
testcase_03 AC 6 ms
12,820 KB
testcase_04 AC 6 ms
12,820 KB
testcase_05 AC 6 ms
13,064 KB
testcase_06 AC 6 ms
12,856 KB
testcase_07 AC 6 ms
12,988 KB
testcase_08 AC 6 ms
12,840 KB
testcase_09 AC 6 ms
13,044 KB
testcase_10 AC 6 ms
12,848 KB
testcase_11 AC 6 ms
12,908 KB
testcase_12 AC 6 ms
12,876 KB
testcase_13 AC 6 ms
13,136 KB
testcase_14 AC 6 ms
13,084 KB
testcase_15 AC 7 ms
12,864 KB
testcase_16 AC 7 ms
12,912 KB
testcase_17 AC 7 ms
13,016 KB
testcase_18 AC 7 ms
12,880 KB
testcase_19 AC 6 ms
12,888 KB
testcase_20 AC 7 ms
12,848 KB
testcase_21 AC 7 ms
12,844 KB
testcase_22 AC 7 ms
12,888 KB
testcase_23 AC 7 ms
12,932 KB
testcase_24 AC 6 ms
12,916 KB
testcase_25 AC 7 ms
12,912 KB
testcase_26 AC 6 ms
12,980 KB
testcase_27 AC 7 ms
12,936 KB
testcase_28 AC 6 ms
12,852 KB
testcase_29 AC 7 ms
12,876 KB
testcase_30 AC 7 ms
12,868 KB
testcase_31 AC 7 ms
12,848 KB
testcase_32 AC 5 ms
12,772 KB
testcase_33 AC 6 ms
12,808 KB
testcase_34 AC 6 ms
12,948 KB
testcase_35 AC 8 ms
12,868 KB
testcase_36 AC 8 ms
12,928 KB
testcase_37 AC 8 ms
13,148 KB
testcase_38 AC 8 ms
12,956 KB
testcase_39 AC 8 ms
12,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}

struct edge{ int to, cost; };

#define MAX_N 200005
int nn = MAX_N; // 複数回根付き木を作成する場合は適宜変える.
vector<edge> pre_G[MAX_N];
vector<edge> G[MAX_N];
int par_[MAX_N];
int flag_netsukigi[MAX_N];
void make_netsukigi(int r){
    rep(i,nn){
        G[i].clear();
        par_[i] = 0;
        flag_netsukigi[i] = 0;
    }
    queue<int> que;
    que.push(r);
    flag_netsukigi[r] = 1;
    par_[r] = -1;

    while(que.size() > 0){
        int x = que.front();
        que.pop();
        flag_netsukigi[x] = 1;
        rep(i,pre_G[x].size()){
            if(flag_netsukigi[pre_G[x][i].to] == 0){
                par_[pre_G[x][i].to] = x;
                G[x].push_back(pre_G[x][i]);
                que.push(pre_G[x][i].to);
            }
        }
    }
}

int a[310];
int n, m;
vector<int> dfs(int x, vector<int> dp){
    vector<int> res = dp;
    rep(i,m+1){
        dp[i] = dp[i] + a[x];
    }

    rep(i,G[x].size()){
        int y = G[x][i].to;
        int cost = G[x][i].cost;
        vector<int> dp2 = dfs(y, dp);
        drep(i,m+1){
            if(i + cost*2 <= m){
                dp[i+cost*2] = max(dp[i+cost*2], dp2[i]);
            }
        }
    }

    return dp;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    cin >> n >> m;
    nn = n;

    
    rep(i,n) cin >> a[i];

    rep(i,n-1){
        int from, to, cost;
        cin >> from >> to >> cost;
        // from--; to--; // 0-indexにするため.
        edge e1, e2;
        e1.to = to; e2.to = from;
        e1.cost = cost; e2.cost = cost;
        pre_G[from].push_back(e1);
        pre_G[to].push_back(e2);
    }

    make_netsukigi(0);

    vector<int> dp(3100);
    rep(i,3100) dp[i] = 0;

    dp = dfs(0, dp);
    int ans = 0;
    rep(i,m+1) ans = max(ans, dp[i]);

    cout << ans << endl;
    return 0;
}


0