Codeforces 815C. Karen and Supermarket【树形DP】
生活随笔
收集整理的這篇文章主要介紹了
Codeforces 815C. Karen and Supermarket【树形DP】
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
LINK
思路
首先發(fā)現(xiàn)依賴關(guān)系是一個(gè)樹(shù)形的結(jié)構(gòu)
然后因?yàn)橹苯铀慊ǘ嗌馘X(qián)來(lái)統(tǒng)計(jì)貢獻(xiàn)不是很好
因?yàn)閿?shù)組開(kāi)不下
那就可以算一個(gè)子樹(shù)里面選多少個(gè)的最小代價(jià)就可以了
注意統(tǒng)計(jì)貢獻(xiàn)的時(shí)候用優(yōu)惠券的答案只能在1號(hào)點(diǎn)進(jìn)行統(tǒng)計(jì)
//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; }
轉(zhuǎn)載于:https://www.cnblogs.com/dream-maker-yk/p/9760186.html
總結(jié)
以上是生活随笔為你收集整理的Codeforces 815C. Karen and Supermarket【树形DP】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ACM-ICPC 2018 沈阳赛区现场
- 下一篇: Decoder is not a @Sh