loj#2510. 「AHOI / HNOI2018」道路 记忆化,dp

题目链接

https://loj.ac/problem/2510

思路

f[i][a][b]表示到i时,公路个数a,铁路个数b
记忆化
复杂度=状态数=\(nlog^2n\)

代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+7;
int read() {
	int x=0,f=1;char s=getchar();
	for(;s>'9'||s<'0';s=getchar()) if(s=='-') f=-1;
	for(;s>='0'&&s<='9';s=getchar()) x=x*10+s-'0';
	return x*f;
}
ll n,f[20007][41][41];
ll ls[N],rs[N],a[N],b[N],c[N];
ll dfs(int u,int A,int B) {
	if(u<0) return c[-u]*(a[-u]+A)*(b[-u]+B);
	if(f[u][A][B]!=0x3f3f3f3f3f3f3f3f) return f[u][A][B];
	f[u][A][B]=min(dfs(ls[u],A+1,B)+dfs(rs[u],A,B),dfs(ls[u],A,B)+dfs(rs[u],A,B+1));
	return f[u][A][B];
}
int main() {
	memset(f,0x3f,sizeof(f));
	n=read();
	for(int i=1;i<n;++i) ls[i]=read(),rs[i]=read();
	for(int i=1;i<=n;++i) a[i]=read(),b[i]=read(),c[i]=read();
	cout<<dfs(1,0,0);
	return 0;
}
posted @ 2019-02-24 11:06  ComplexPug  阅读(148)  评论(0编辑  收藏  举报