传送门

Luogu

解题思路

考虑树形 \(\text{DP}\)

设状态 \(dp[u][i][j]\) 表示从首都走到点 \(u\) ,经过 \(i\) 条公路,\(j\) 条铁路的最小不方便值。

对于叶子节点也就是村庄,直接初始化,对于非叶子节点也就是城市,就从两个儿子向上转移。

而现在的一个问题就是:我们的 \(dp\) 数组根本开不下。

我们不得不优化空间。

考虑到一个性质:每次的答案都是从儿子向上合并,合并过后的节点就没用了。

所以我们只需要在dfs的过程中给每个节点记一个 \(dfn\),满足:

\(dfn_{ls}=dfn_{fa} + 1, dfn_{rs}=dfn_{fa} + 2\)

我们把空间回收利用起来,很容易计算 \(dp\) 的第一位,只需要开到 \(2*40+1=81\) 即可。

这样就可以愉快的 DP 了,答案就是 \(dp[1][0][0]\)

细节注意事项

  • 咕咕咕

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= c == '-', c = getchar();
while (isdigit(c)) s = s * 10 + c - 48, c = getchar();
s = f ? -s : s;
} typedef long long LL;
const int _ = 20010; int n, a[_ << 1], b[_ << 1], c[_ << 1], ch[2][_];
LL dp[90][50][50]; inline void dfs(int s, int u, int x, int y) {
if (u >= n) {
for (rg int i = 0; i <= x; ++i)
for (rg int j = 0; j <= y; ++j)
dp[s][i][j] = 1ll * c[u] * (a[u] + i) * (b[u] + j);
return;
}
dfs(s + 1, ch[0][u], x + 1, y);
dfs(s + 2, ch[1][u], x, y + 1);
int ls = s + 1, rs = s + 2;
for (rg int i = 0; i <= x; ++i)
for (rg int j = 0; j <= y; ++j)
dp[s][i][j] = min(dp[ls][i + 1][j] + dp[rs][i][j], dp[ls][i][j] + dp[rs][i][j + 1]);
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n);
for (rg int i = 1; i < n; ++i) {
read(ch[0][i]), ch[0][i] = ch[0][i] < 0 ? n - ch[0][i] - 1 : ch[0][i];
read(ch[1][i]), ch[1][i] = ch[1][i] < 0 ? n - ch[1][i] - 1 : ch[1][i];
}
for (rg int i = 1; i <= n; ++i)
read(a[i + n - 1]), read(b[i + n - 1]), read(c[i + n - 1]);
dfs(1, 1, 0, 0);
printf("%lld\n", dp[1][0][0]);
return 0;
}

完结撒花 \(qwq\)

最新文章

  1. Boostrap入门(一)
  2. linux系统下根据端口查看进程
  3. 【原创】本地通过IIS设置开发的localhost网站的域名改为个性域名方法
  4. Java Concurrency In Practice -Chapter 2 Thread Safety
  5. js DOM的几个常用方法
  6. PF_RING 总结
  7. 您可能不知道的ASP.Net小技巧
  8. JSP页面的构成
  9. 使用jQuery UI方法
  10. 虽然net人
  11. CodeForces 606B Testing Robots
  12. 笑谈ArcToolbox (3) ArcToolbox的一亩三分地
  13. msp430系统时钟
  14. pstree:command not found
  15. NowCoder--牛客练习赛30 C_小K的疑惑
  16. 基于Docker搭建MySQL多源复制环境
  17. zoj3956(Course Selection System)_Solution
  18. ps p图
  19. [java]转:String Date Calendar之间的转换
  20. numpy.ravel() vs numpy.flatten()

热门文章

  1. Linux终端的一些快捷键命令
  2. MySQL高可用之MHA配置
  3. 5G时代开启,这些新兴职业决定你的后半生
  4. 解决sublime不能安装packages的问题
  5. 【转载】五分钟让你彻底了解TDD、ATDD、BDD&amp;RBE
  6. iOS 开发之 RunLoop 详解
  7. subprocess.run()用法python3.7
  8. vagrant up启动centos7时出现&quot;rsync&quot; could not be found on your PATH. Make sure that rsyncis properly ins
  9. java8新特性1:lambda表达式和函数式接口
  10. pikachu-字符型注入(get) #手工注入