結果

問題 No.417 チューリップバブル
ユーザー Hachimori
提出日時 2016-08-27 12:17:05
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
TLE  
実行時間 -
コード長 1,765 bytes
コンパイル時間 779 ms
コンパイル使用メモリ 64,864 KB
実行使用メモリ 41,424 KB
最終ジャッジ日時 2024-11-08 19:07:36
合計ジャッジ時間 4,945 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 35 TLE * 1 -- * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#define dst first
#define time second 
using namespace std;
typedef pair<int, int> Edge;
const int NODE = 105;
const int TIME = 1005;


int nNode, limit;
int val[NODE];
vector<Edge> adj[NODE];

void read() {
    for (int i = 0; i < NODE; ++i) adj[i].clear();

    cin >> nNode >> limit;
    for (int i = 0; i < nNode; ++i) cin >> val[i];

    for (int i = 0; i < nNode - 1; ++i) {
        int s, t, c;
        cin >> s >> t >> c;
        adj[s].push_back(Edge(t, c));
        adj[t].push_back(Edge(s, c));
    }

    limit /= 2;
}


void rec(int prev, int curr, int dp[NODE][TIME]) {

    for (int i = 0; i < adj[curr].size(); ++i) {
        Edge &e = adj[curr][i];
        if (e.dst == prev) continue;
        rec(curr, e.dst, dp);
    }

    dp[curr][0] = val[curr];
    for (int i = 0; i < adj[curr].size(); ++i) {
        Edge &e = adj[curr][i];
        if (e.dst == prev) continue;

        for (int curUsedTime = limit; curUsedTime >= 0; --curUsedTime) {
            if (dp[curr][curUsedTime] == -1) continue;
            for (int nexUsedTime = limit - curUsedTime - e.time; nexUsedTime >= 0; --nexUsedTime) {
                if (dp[e.dst][nexUsedTime] == -1) continue;
                
                int nextValue = dp[curr][curUsedTime] + dp[e.dst][nexUsedTime];
                dp[curr][curUsedTime + nexUsedTime + e.time] = max(dp[curr][curUsedTime + nexUsedTime + e.time], nextValue);
            }
        }
    }
}


void work() {
    // dp[node][used time] := maximum value
    static int dp[NODE][TIME];
    memset(dp, -1, sizeof(dp));
    rec(-1, 0, dp);

    cout << *max_element(dp[0], dp[0] + limit + 1) << endl;
}


int main() {
    read();
    work();
    return 0;
}
0