[HNOI/AHOI2018] Road, the third simulation game, tree DP

The subject

      [HNOI/AHOI2018] Road

      After thinking about this question for a long time, I can't get a little bit of greed...

      In fact, this question sees that " any village can reach the capital through no more than 40 roads. " Therefore, the maximum depth of this question is 40 layers, that is to say, the sum of the railways and highways on each rural path is 40 . strip.

      So we have to guess its time complexity.

      20000*40*40=32000000. Actually not that many.

     Memoized search is what we call tree DP.

      Let's think about whether the two edges of a node are only related to the leaf nodes in the subtree, but have nothing to do with the leaf nodes outside.

      Then we use f[i][j][k] to represent the i-th point, the minimum value generated after the j roads and k railways are not renovated on it .

      f[i][j][k] is obviously equal to the minimum of choosing left overhaul and choosing right overhaul.

      Conversely, the right side is not renovated, and the left side is not renovated.

      Therefore, the state can be continuously passed down to find the minimum value.

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;

int n;
long long a[20010],b[20010],c[20010];
int son[40010][2];
long long f[30010][41][41];

long long dfs(int x,int y,int z){
	if(son[x][0]==0) return c[x-(n-1)]*(a[x-(n-1)]+y)*(b[x-(n-1)]+z);
	if(f[x][y][z]!=f[20009][40][40]) return f[x][y][z];
	return f[x][y][z]=min(dfs(are[x][0],y+1,z)+dfs(are[x][1],y,z),dfs(are[ x][0],y,z)+dfs(are[x][1],y,z+1));
}

int main(){
//	freopen("road.in","r",stdin);
//	freopen("road.out","w",stdout);
	scanf("%d",&n);
	memset(f,63,sizeof(f));
	for(int i=1;i<=n-1;i++){
		int x,y;
		scanf("%d %d",&x,&y);
		if(x>0) son[i][0]=x;
		else son[i][0]=-x+n-1;
		if(y>0) son[i][1]=y;
		else are[i][1]=-y+n-1;
	}
	for(int i=1;i<=n;i++) scanf("%lld %lld %lld",&a[i],&b[i],&c[i]);
	printf("%lld",dfs(1,0,0));
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325977970&siteId=291194637