[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\n",f[1][0][0]);return 0;
}

转载于:https://www.cnblogs.com/zhoushuyu/p/8869795.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值