【BZOJ5290】【HNOI2018】道路(树形DP+滑稽卡空间)

Description

click me


Solution

我考场上居然傻逼到这种题都没写出来。。还是太菜了。。。

考虑树形DP,设 d p u , i , j 表示节点 u 到根的路径上有 i 条公路、 j 条铁路,转移非常显然,如果不知道就看代码吧。
先在洛谷上A掉了,然后在BZOJ上愉快地发现MLE了。
我懒(kan)得(bu)看(dong)网上的大佬是怎么卡空间的,然后加上了这个:

#define dp(u, i, j) (u >= n ? (c[u - n + 1] * (a[u - n + 1] + i) * (b[u - n + 1] + j)) : dp[u][i][j])

然后就A掉了233333333333333


Code

/************************************************
 * Au: Hany01
 * Date: Apr 17th, 2018
 * Prob: [BZOJ5290][HNOI2018] 道路
 * Email: [email protected]
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 20005, maxd = 41;

int n, a[maxn], b[maxn], ch[maxn][2];
LL c[maxn], dp[maxn][maxd][maxd];

#define dp(u, i, j) (u >= n ? (c[u - n + 1] * (a[u - n + 1] + i) * (b[u - n + 1] + j)) : dp[u][i][j])

void dfs(int u, int d1, int d2)
{
    if (u >= n) return ;
    dfs(ch[u][0], d1 + 1, d2), dfs(ch[u][1], d1, d2 + 1);
    For(i, 0, d1) For(j, 0, d2)
        dp[u][i][j] = min(dp(ch[u][0], i + 1, j) + dp(ch[u][1], i, j), dp(ch[u][0], i, j) + dp(ch[u][1], i, j + 1));
}

int main()
{
#ifdef hany01
    File("bzoj5290");
#endif

    static int si, ti;

    n = read();
    For(i, 1, n - 1)
    {
        si = read(), ti = read();
        if (si < 0) si = n - 1 - si;
        if (ti < 0) ti = n - 1 - ti;
        ch[i][0] = si, ch[i][1] = ti;
    }
    For(i, 1, n)
        a[i] = read(), b[i] = read(), c[i] = read();

    dfs(1, 0, 0);
    printf("%lld\n", dp[1][0][0]);

    return 0;
}
//春未绿,鬓先丝。人间别久不成悲。
//    -- 姜夔《鹧鸪天·元夕有所梦》

猜你喜欢

转载自blog.csdn.net/hhaannyyii/article/details/79994341