您的位置:首页 > 其它

洛谷 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: