日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Codeforces 815C. Karen and Supermarket【树形DP】

發布時間:2025/3/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces 815C. Karen and Supermarket【树形DP】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

LINK


思路

首先發現依賴關系是一個樹形的結構
然后因為直接算花多少錢來統計貢獻不是很好
因為數組開不下
那就可以算一個子樹里面選多少個的最小代價就可以了

注意統計貢獻的時候用優惠券的答案只能在1號點進行統計


//Author: dream_maker #include<bits/stdc++.h> using namespace std; //---------------------------------------------- //typename typedef long long ll; //convenient for #define fu(a, b, c) for (int a = b; a <= c; ++a) #define fd(a, b, c) for (int a = b; a >= c; --a) #define fv(a, b) for (int a = 0; a < (signed)b.size(); ++a) //inf of different typename const int INF_of_int = 1e9; const ll INF_of_ll = 1e18; //fast read and write template <typename T> void Read(T &x) {bool w = 1;x = 0;char c = getchar();while (!isdigit(c) && c != '-') c = getchar();if (c == '-') w = 0, c = getchar();while (isdigit(c)) {x = (x<<1) + (x<<3) + c -'0';c = getchar();}if (!w) x = -x; } template <typename T> void Write(T x) {if (x < 0) {putchar('-');x = -x; }if (x > 9) Write(x / 10);putchar(x % 10 + '0'); } //---------------------------------------------- const int N = 5010; struct Edge{int v, nxt; }E[N << 1]; int head[N], tot = 0; ll c[N], d[N], siz[N], n; ll dp[N][N][2], ans = 0, B; void add(int u, int v) {E[++tot] = (Edge){v, head[u]};head[u] = tot; } void dfs(int u, int fa) {siz[u] = 1;dp[u][1][1] = c[u] - d[u];dp[u][1][0] = c[u];for (int i = head[u]; i; i = E[i].nxt) {int v = E[i].v;if (v == fa) continue;dfs(v, u);fd(j, siz[u], 0)fd(k, siz[v], 0) {dp[u][j + k][1] = min(dp[u][j + k][1], dp[u][j][1] + min(dp[v][k][0], dp[v][k][1]));dp[u][j + k][0] = min(dp[u][j + k][0], dp[u][j][0] + dp[v][k][0]); }siz[u] += siz[v];}fu(i, ans + 1, siz[u]) {if (dp[u][i][0] <= B) ans = i;else break;} } int main() {memset(dp, 0x3f, sizeof(dp));Read(n); Read(B);fu(i, 1, n) {Read(c[i]); Read(d[i]);dp[i][0][0] = 0;if (i > 1) {int u; Read(u);add(i, u);add(u, i);}}dfs(1, 0);fu(i, ans + 1, n) if (dp[1][i][1] <= B) ans = i;Write(ans);return 0; }

轉載于:https://www.cnblogs.com/dream-maker-yk/p/9760186.html

總結

以上是生活随笔為你收集整理的Codeforces 815C. Karen and Supermarket【树形DP】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。