洛谷 2014 选课
2017-08-28 17:49
218 查看
//dp[i][j]是以i为根不算i的子树选j门课的学分 #include<bits/stdc++.h> using namespace std; const int maxn = 3030; int n, m, tot = 0; int dp[maxn][maxn], st[maxn], a[maxn]; struct node{ int v, w, nxt; } edge[maxn]; inline void in(int x, int y){ edge[++tot].v = y; //edge[tot].w = z; edge[tot].nxt = st[x]; st[x] = tot; } inline void DP(int now, int step){ if(step <= 0) return; for(int i = st[now]; i; i = edge[i].nxt){ int to = edge[i].v; for(int j = 1; j <= step; j++) dp[to][j] = dp[now][j];//不选以to子树的情况 DP(to, step-1); for(int j = 1; j <= step; j++) dp[now][j] = max(dp[now][j], dp[to][j-1] + a[to]);//权衡选与不选 } } int main(){ scanf("%d%d", &n, &m); for(int i = 1, x, y; i <= n; i++){ scanf("%d%d", &x, &a[i]); in(x, i); } DP(0, m); //printf("%d\n", dp[1][m]); printf("%d\n", dp[0][m]); return 0; }
相关文章推荐
- 洛谷 P2014 选课
- 洛谷 P2014 选课
- 洛谷—— P2014 选课
- 洛谷 2014 选课 树形DP 解题报告
- 洛谷 P2014 选课
- 洛谷 p2014 [CTSC2001] 选课 树形背包DP
- 洛谷2014选课
- 树形dp-洛谷 P2014 选课
- 【洛谷2014】选课
- 【洛谷2014】选课
- |洛谷|动态规划|P2014 选课
- AC日记——[JLOI2014]松鼠的新家 洛谷 P3258
- 洛谷2375 NOI2014 动物园
- 【Luogu P2014 选课】【树形背包学习笔记】
- BZOJ3505 & 洛谷P3166 [Cqoi2014]数三角形 【数学、数论】
- 【模拟】洛谷 P1328 NOIP2014提高组 day1 T1 生活大爆炸版石头剪刀布
- 【NOIP2014】洛谷2258 子矩阵
- 【NOIP2014】【洛谷1941】【CJOJ1672】飞扬的小鸟
- 乱搞-洛谷P3237 [HNOI2014]米特运输
- BZOJ4543/3522: [POI2014]Hotel加强版(洛谷P3565)