P4438 [HNOI/AHOI2018] 道路 题解
注意到树高度不超过
我们要求的答案也是与路径中的边数有关,不妨考虑 DP,
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
const int N = 4e4 + 5;
int n;
int s1[N], s2[N];
long long f[N][41][41];
long long a[N], b[N], c[N];
vector<int> G[N];
int main()
{
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++)
{
int s, t;
cin >> s >> t;
if (s < 0) s = (-s) + n - 1;
if (t < 0) t = (-t) + n - 1;
s1[i] = s, s2[i] = t;
}
for (int i = n; i <= 2 * n - 1; i++) cin >> a[i] >> b[i] >> c[i];
auto dfs = [&](auto self, int u, int x, int y)->long long
{
if (f[u][x][y]) return f[u][x][y];
if (u > n - 1)
{
return c[u] * (a[u] + x) * (b[u] + y);
}
// think of left or right
// left
long long res = self(self, s1[u], x, y) + self(self, s2[u], x, y + 1);
long long res2 = self(self, s1[u], x + 1, y) + self(self, s2[u], x, y);
return f[u][x][y] = min(res, res2);
};
cout << dfs(dfs, 1, 0, 0) << "\n";
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现