洛谷 2014 选课

//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;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值