其他分享
首页 > 其他分享> > 洛谷 P2014 选课

洛谷 P2014 选课

作者:互联网

洛谷 P2014 选课

Description

Input

Output

Sample Input

7  4
2  2
0  1
0  4
2  1
7  1
7  6
2  2

Sample Output

13

题解:

#include <iostream>
#include <cstdio>
#define N 305
using namespace std;

struct E {int next, to;} e[N];
int n, m, num;
int h[N], in[N], w[N];
int dp[N][N];
bool vis[N];

void add(int u, int v)
{
    e[++num].next = h[u];
    e[num].to = v;
    h[u] = num;
}

void dfs(int x)
{
    vis[x] = 1;
    for(int i = h[x]; i != 0; i = e[i].next)
        if(!vis[e[i].to]) dfs(e[i].to);
    for(int i = h[x]; i != 0; i = e[i].next)
    {
        int now = e[i].to;
        for(int j = m - w[x]; j >= w[now]; j--)
            for(int k = 0; k <= j; k++)
                dp[x][j + w[x]] = max(dp[x][j + w[x]], dp[x][j + w[x] - k] + dp[now][k]);
    }
} 

int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        int to;
        cin >> to >> dp[i][1];
        w[i] = 1, add(to, i);
    }
    dfs(0);
    cout << dp[0][m];
    return 0;
}

标签:洛谷,选课,P2014,num,next,int,课程,修课,dp
来源: https://www.cnblogs.com/BigYellowDog/p/11583738.html