結果
| 問題 |
No.417 チューリップバブル
|
| コンテスト | |
| ユーザー |
wheson
|
| 提出日時 | 2018-05-17 23:46:42 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 315 ms / 2,000 ms |
| コード長 | 1,927 bytes |
| コンパイル時間 | 1,836 ms |
| コンパイル使用メモリ | 175,252 KB |
| 実行使用メモリ | 5,760 KB |
| 最終ジャッジ日時 | 2024-06-28 13:37:52 |
| 合計ジャッジ時間 | 6,324 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 40 |
ソースコード
#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;
}
wheson