洛谷P4438 [HNOI/AHOI2018]道路(dp)

题意

题目链接

Sol

每当出题人想起他出的HNOI 2018 Day2T3,他都会激动的拍打着轮椅

读题比做题用时长系列。。。

(f[i][a][b])表示从根到(i)的路径上,有(a)条公路未被翻修,(b)条铁路未被翻修

然后xjb转移一下

比较好奇为啥不会MLE..

#include<bits/stdc++.h>
#define LL long long 
using namespace std;
const int MAXN = 1e5 + 10;
const LL INF = 1e18;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while (c < '0' || c > '9') {if (c == '-')f = -1; c = getchar();}
    while (c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
    return x * f;
}
int N, ls[MAXN], rs[MAXN];
LL A[MAXN], B[MAXN], C[MAXN];
vector<int> v[MAXN];
LL f[20001][41][41];
LL dfs(int x, LL a, LL b) {
    if(x > N) return C[x] * (A[x] + a) * (B[x] + b);
    if(f[x][a][b] <= INF) return f[x][a][b];
    else f[x][a][b] = min(dfs(ls[x], a, b) + dfs(rs[x], a, b + 1), dfs(ls[x], a + 1, b) + dfs(rs[x], a, b));
    return f[x][a][b];
}
int main() {
//	freopen("a.in", "r", stdin);
    memset(f, 0x7f, sizeof(f));
    N = read();
    for(int i = 1; i <= N - 1; i++) {
        ls[i] = read(); rs[i] = read();
        if(ls[i] < 0) ls[i] = -ls[i] + N;
        if(rs[i] < 0) rs[i] = -rs[i] + N;
    }
    for(int i = N + 1; i <= 2 * N; i++) A[i] = read(), B[i] = read(), C[i] = read();
    if(N == 1) cout << 0 << endl;
 	else cout << dfs(1, 0, 0);
    return 0;
}
原文地址:https://www.cnblogs.com/zwfymqz/p/10424148.html