[BZOJ5290][HNOI2018]道路

bzoj
luogu

sol

考场上普及(dp)都做不来,果然是思想僵化了。
(f[u][i][j])表示在(u)点,上方有(i)条未修复的公路和(j)条未修复的铁路的最小花费。
转移只有两种情况,对于修公路和修铁路分开处理即可。
复杂度(O(n*40^2))。可以卡一下空间,也就是乡村的(dp)值不用数组存而是现算。

code

#include<cstdio>
#include<algorithm>
using namespace std;
int gi()
{
	int x=0,w=1;char ch=getchar();
	while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
	if (ch=='-') w=0,ch=getchar();
	while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
	return w?x:-x;
}
#define ll long long
const int N = 40005;
int n,s[N],t[N],a[N],b[N],c[N];
ll f[N>>1][41][41];
ll dp(int u,int i,int j)
{
	if (u<n) return f[u][i][j];
	else return 1ll*c[u]*(a[u]+i)*(b[u]+j);
}
void dfs(int u,int dep)
{
	if (u>=n) return;
	dfs(s[u],dep+1);dfs(t[u],dep+1);
	for (int i=0;i<=dep;++i)
		for (int j=0;j<=dep;++j)
			f[u][i][j]=min(dp(s[u],i+1,j)+dp(t[u],i,j),dp(s[u],i,j)+dp(t[u],i,j+1));
}
int main()
{
	n=gi();
	for (int i=1;i<n;++i)
	{
		s[i]=gi();t[i]=gi();
		if (s[i]<0) s[i]=-s[i]+n-1;
		if (t[i]<0) t[i]=-t[i]+n-1;
	}
	for (int i=n;i<n+n;++i) a[i]=gi(),b[i]=gi(),c[i]=gi();
	dfs(1,0);printf("%lld
",f[1][0][0]);return 0;
}
原文地址:https://www.cnblogs.com/zhoushuyu/p/8869795.html