結果

問題 No.417 チューリップバブル
ユーザー keita1_atcoderkeita1_atcoder
提出日時 2022-05-27 00:15:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,004 ms / 2,000 ms
コード長 1,150 bytes
コンパイル時間 5,062 ms
コンパイル使用メモリ 186,808 KB
実行使用メモリ 6,728 KB
最終ジャッジ日時 2023-10-20 19:36:35
合計ジャッジ時間 13,083 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 9 ms
4,348 KB
testcase_09 AC 20 ms
4,348 KB
testcase_10 AC 22 ms
4,348 KB
testcase_11 AC 113 ms
4,348 KB
testcase_12 AC 132 ms
4,348 KB
testcase_13 AC 16 ms
4,348 KB
testcase_14 AC 176 ms
4,356 KB
testcase_15 AC 14 ms
4,348 KB
testcase_16 AC 13 ms
4,348 KB
testcase_17 AC 106 ms
4,372 KB
testcase_18 AC 110 ms
4,372 KB
testcase_19 AC 113 ms
4,372 KB
testcase_20 AC 452 ms
5,148 KB
testcase_21 AC 424 ms
5,148 KB
testcase_22 AC 376 ms
5,148 KB
testcase_23 AC 450 ms
5,148 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 424 ms
5,148 KB
testcase_26 AC 36 ms
4,348 KB
testcase_27 AC 319 ms
4,672 KB
testcase_28 AC 410 ms
5,148 KB
testcase_29 AC 434 ms
5,148 KB
testcase_30 AC 421 ms
5,148 KB
testcase_31 AC 434 ms
5,148 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 5 ms
4,348 KB
testcase_34 AC 33 ms
4,356 KB
testcase_35 AC 919 ms
6,728 KB
testcase_36 AC 941 ms
6,728 KB
testcase_37 AC 860 ms
6,728 KB
testcase_38 AC 926 ms
6,728 KB
testcase_39 AC 1,004 ms
6,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
vector<long long> U;

struct Edge{
    int to,w;
    Edge(int a,int b){
        to = a;
        w = b;
    }
    Edge(){}
};

vector<vector<Edge>> G;

vector<vector<vector<long long>>> memo;
long long DFS(int v,int a,int t,int p = -1){
    if(a<0) return 0;
    if(G[v][a].to==p) return DFS(v,a-1,t,p);
    if(memo[v][a][t/2]!=-1) return memo[v][a][t/2];
    long long res = DFS(v,a-1,t,p);
    for(int j = 0;j <= (t-2*G[v][a].w);j+=2){
        res = max(res,DFS(v,a-1,t-2*G[v][a].w-j,p)+DFS(G[v][a].to,(int)G[G[v][a].to].size()-1,j,v)+U[G[v][a].to]);
    }
    memo[v][a][t/2] = res;
    return res;
}

int main(){
    int N,M;
    cin >> N >> M;
    if(M%2) M--;
    U.assign(N,0);
    for(int i = 0;i < N;i++) cin >> U[i];
    G.assign(N,vector<Edge>(0));
    for(int i = 0;i < N-1;i++){
        int A,B,C;
        cin >> A >> B >> C;
        G[A].push_back(Edge(B,C));
        G[B].push_back(Edge(A,C));
    }
    memo.assign(N,vector<vector<long long>>(0));
    for(int i = 0;i < N;i++) memo[i].assign(G[i].size(),vector<long long>(M/2+1,-1));
    cout << DFS(0,(int)G[0].size()-1,M)+U[0] << endl;
}
0