結果

問題 No.3283 Labyrinth and Friends
ユーザー t98slider
提出日時 2025-09-26 22:34:50
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 1,527 bytes
コンパイル時間 2,166 ms
コンパイル使用メモリ 206,532 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-09-26 22:34:54
合計ジャッジ時間 3,593 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template<class T> istream& operator >> (istream& is, vector<T>& vec) {
    for(T& x : vec) is >> x;
    return is;
}

template<class T> ostream& operator << (ostream& os, const vector<T>& vec) {
    if(vec.empty()) return os;
    os << vec[0];
    for(auto it = vec.begin(); ++it != vec.end(); ) os << ' ' << *it;
    return os;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, x;
    cin >> n >> x;
    vector<vector<int>> g(n);
    for(int i = 1; i < n; i++){
        int p;
        cin >> p;
        g[p - 1].emplace_back(i);
    }
    vector<pair<int,int>> a(n);
    for(int i = 1; i < n; i++){
        auto &&[c, s] = a[i];
        cin >> c >> s;
    }
    vector<vector<ll>> dp(n);
    auto dfs = [&](auto dfs, int v) -> void {
        auto&& tmp = dp[v];
        tmp.resize(1, 0);
        for(auto u : g[v]){
            dfs(dfs, u);
            auto [c, s] = a[u];
            int m = tmp.size() + s + dp[u].size() - 1;
            vector<ll> ndp(m, 1ll << 60);
            for(int i = 0; i < tmp.size(); i++){
                ndp[i] = min(ndp[i], tmp[i]);
                for(int j = 0; i + j + s < m && j < dp[u].size(); j++){
                    ndp[i + j + s] = min(ndp[i + j + s], tmp[i] + dp[u][j] + c);
                }
            }
            swap(ndp, tmp);
        }
        for(int i = tmp.size() - 1; i >= 1; i--) tmp[i - 1] = min(tmp[i - 1], tmp[i]);
    };
    dfs(dfs, 0);
    cout << dp[0][x] << '\n';
}
0