CH 5402 코스 (가방 + 나무 패킷 DP)

CH 5402 코스



\(해결책:\)

질문의 매우 일상적인 논의는 트리 구조가 포함 된 다른 코스 (여기 또한 다스 나무로 가상 숲을 구축 할 포인트) 사이의 관계를 보여줍니다 사용합니다. 그들은 DP 생산 관계가 포함되어 있으므로, 그 후 제거 개념 하위 트리 영리한 후유증을 사용한다. 우리는 하위 트리 전체 배낭에 파이프에 갈 필요하기 전에 필요한 과정을, 다음 패킷 배낭 각 노드는 이동하는 전체 최적의 솔루션의 지역 확산을 만난다.



\(암호:\)

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>

#define ll long long
#define db double
#define rg register int

using namespace std;

int n,m,ff,top;
int a[305];
int f[305][305];

int tou[305];
struct su{
    int to,next;
}b[305];

inline int qr(){
    register char ch; register bool sign=0; rg res=0;
    while(!isdigit(ch=getchar())) if(ch=='-')sign=1;
    while(isdigit(ch)) res=res*10+(ch^48),ch=getchar();
    return sign?-res:res;
}

inline void add(int x,int y){
    b[++top].to=y; b[top].next=tou[x]; tou[x]=top;
}

inline void dfs(int i){
    for(rg j=tou[i];j;j=b[j].next){
        rg to=b[j].to; dfs(to);
        for(rg p=m;p>0;--p)
            for(rg q=0;q<p;++q)
                f[i][p]=max(f[i][p],f[i][q]+f[to][p-q]);
    }if(i)for(rg k=m;k>0;--k)f[i][k]=f[i][k-1]+a[i];
}

int main(){
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    n=qr(); m=qr();
    for(rg i=1;i<=n;++i)
        add(qr(),i),a[i]=qr();
    dfs(0); printf("%d\n",f[0][m]);
    return 0;
}

추천

출처www.cnblogs.com/812-xiao-wen/p/11016409.html