結果
問題 |
No.3283 Labyrinth and Friends
|
ユーザー |
|
提出日時 | 2025-09-26 23:48:58 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 61 ms / 2,000 ms |
コード長 | 997 bytes |
コンパイル時間 | 1,132 ms |
コンパイル使用メモリ | 95,392 KB |
実行使用メモリ | 74,240 KB |
最終ジャッジ日時 | 2025-09-26 23:49:04 |
合計ジャッジ時間 | 5,267 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 45 |
ソースコード
#include <iostream> #include <vector> using namespace std; using ll = long long; int K = 3000; vector<vector<int>> G(K); vector<ll> C(K, 0); vector<int> S(K, 0); ll inf = (1LL << 60); vector<vector<ll>> dp(K+1, vector<ll>(K+1, inf)); int dfs(int v, int par) { int sub = S[v]; vector<ll> DP(1, 0); for (auto nv : G[v]) { if (nv == par) continue; int M = dfs(nv, v); sub += M; vector<ll> ndp(DP.size()+M,inf); for (int i = 0;i < DP.size();i++) for (int j = 0;j <= M;j++) { if (DP[i] == inf or dp[nv][j] == inf) continue; ndp[i+j] = min(ndp[i+j], DP[i]+dp[nv][j]); } swap(ndp, DP); } for (int i = 0;i < DP.size();i++) { dp[v][i+S[v]] = DP[i] + C[v]; } dp[v][0] = 0; return sub; } int main() { int N, X;cin >> N >> X; for (int i = 1;i < N;i++) { int a;cin >> a; a--; G[i].push_back(a); G[a].push_back(i); } for (int i = 1;i < N;i++) cin >> C[i] >> S[i]; dfs(0, 0); ll ans = inf; for (int t = X;t <= K;t++) ans = min(ans, dp[0][t]); cout << ans << endl; }