jzoj5819. 【NOIP提高A组模拟2018.8.15】 大逃杀

题意略。
我太弱了,搞了好久,才搞懂题解,心态崩ing

solution

f i , j 表示以i为根的子树中,从i开始走,最后回到i,用j时间,所能拿到的最大武力值。
g i , j 表示以i为根的子树中,从i开始走,不回到i,用j时间,所能拿到的最大武力值。
h i , j 表示以i为根的子树中,从i的子树内开始走,经过i,最后还是在i的子树内,用j时间,所能拿到的最大武力值。
考虑f,g的转移
设c表示x走到son这条边的时间
f x , j = f x , k + f s o n , j k 2 c
g x , j = { g x , k + f s o n , j k 2 c f x , k + g s o n , j k c
h的转移稍微复杂
1.
这里写图片描述
h[x][j]=max(h[x][j],h[x][k]+f[son][j-k-2*c]);
相当于从son的某棵子树中y,走上来x,走下去x的某棵子树k,再从k的子树中走上来x,再从x走回到son的某棵子树z,实际上我们是把h这条路拆开了。
2.h[x][j]=max(h[x][j],f[x][k]+h[son][j-k-2*c]);
3. h[x][j]=max(h[x][j],g[x][k]+g[son][j-k-c]);相当于是将两条路接起来

#include<cstdio>
#define fo(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define fd(i,b,a) for(int (i)=(b);(i)>=(a);(i)--)
#define Fo(i,x) for (int i=head[x];i;i=next[i])
#define max(x,y) ((x)>(y)?(x):(y))
using namespace std;
typedef long long ll;
const int N=305;
const int inf=1e+9;
int f[N][N],g[N][N],h[N][N],w[N],t[N],n,m,head[N],to[N*2],next[N*2],v[N*2],tot;
void R(int &n)
{
    int t=0,p=1;char ch;
    for(ch=getchar();!('0'<=ch && ch<='9');ch=getchar())
        if(ch=='-') p=-1;
    for(;'0'<=ch && ch<='9';ch=getchar()) t=t*10+ch-'0';
    n=t*p;
}
void add(int x,int y,int z){
    to[++tot]=y;
    next[tot]=head[x];
    v[tot]=z;
    head[x]=tot;
}
void dfs(int x,int y){
    fo(i,0,t[x]-1) f[x][i]=g[x][i]=h[x][i]=-inf;
    fo(i,t[x],m) f[x][i]=g[x][i]=h[x][i]=w[x];
    Fo(i,x){
        int son=to[i],c=v[i];
        if (son==y) continue;
        dfs(son,x);
        fd(j,m,c)
        fd(k,j-c,0)
        {
            if (j-k-2*c>=0)
            {
                h[x][j]=max(h[x][j],h[x][k]+f[son][j-k-2*c]);
                h[x][j]=max(h[x][j],f[x][k]+h[son][j-k-2*c]);
            }
            h[x][j]=max(h[x][j],g[x][k]+g[son][j-k-c]);
            if (j-k-2*c>=0)
                g[x][j]=max(g[x][j],g[x][k]+f[son][j-k-2*c]);
            g[x][j]=max(g[x][j],f[x][k]+g[son][j-k-c]);
            if (j-k-2*c>=0)
                f[x][j]=max(f[x][j],f[x][k]+f[son][j-k-2*c]);
        }
    }
}
int main(){
    freopen("toyuq.in","r",stdin);
    freopen("toyuq.out","w",stdout);
    R(n);R(m);
    bool flag=0;
    fo(i,1,n){
        R(w[i]);
        if (w[i]) flag=1;
    }
    if (!flag){
        printf("%d",0);
        return 0;
    }
    fo(i,1,n) R(t[i]);
    int x,y,z;
    fo(i,1,n-1){
        R(x);R(y);R(z);
        add(x,y,z);add(y,x,z);
    }
    dfs(1,1);
    int ans=0;
    fo(i,1,n)  
        ans=max(ans,h[i][m]);
    printf("%d",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ganjingxian/article/details/81745192
今日推荐