当前位置: 首页 > article >正文

洛谷4438 [Hnoi2018]道路 【树形dp】

题目

题目太长懒得打

题解

HNOI2018惊现普及+/提高?
由最长路径很短,设\(f[i][x][y]\)表示\(i\)号点到根有\(x\)条未修公路,\(y\)条未修铁路,子树所有乡村不便利值的最小值
如果\(i\)为乡村,直接等于公式
如果\(i\)不为乡村,枚举修哪边儿子
\(f[i][x][y] = min\{f[ls][x + 1][y] + f[rs][x][y],f[ls][x][y] + f[rs][x][y + 1]\}\)

\(ans = f[1][0][0]\)
很多\(f\)用完就丢了,可以滚一下数组省空间

完了

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 40005,maxm = 100005,INF = 1000000000;
inline int read(){int out = 0,flag = 1; char c = getchar();while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}return out * flag;
}
LL f[110][41][41];
int n,s[maxn],t[maxn],a[maxn],b[maxn],c[maxn],dep[maxn];
int st[maxn],top,id[maxn];
void dfs(int u){if (u >= n){int x = u - n + 1; id[u] = st[top--];for (int i = 0; i <= dep[u]; i++)for (int j = 0; j <= dep[u]; j++)f[id[u]][i][j] = 1ll * c[x] * (a[x] + i) * (b[x] + j);return;}dep[s[u]] = dep[t[u]] = dep[u] + 1;dfs(s[u]); dfs(t[u]);id[u] = st[top--];for (int i = 0; i <= dep[u]; i++)for (int j = 0; j <= dep[u]; j++)f[id[u]][i][j] = min(f[id[s[u]]][i][j] + f[id[t[u]]][i][j + 1],f[id[s[u]]][i + 1][j] + f[id[t[u]]][i][j]);st[++top] = id[s[u]]; st[++top] = id[t[u]];
}
int main(){n = read();for (int i = 1; i < n; i++){s[i] = read(); t[i] = read();if (s[i] < 0) s[i] = -s[i] + n - 1;if (t[i] < 0) t[i] = -t[i] + n - 1;}for (int i = 1; i <= n; i++) a[i] = read(),b[i] = read(),c[i] = read();top = 99;REP(i,top) st[i] = i;dfs(1);printf("%lld\n",f[id[1]][0][0]);return 0;
}

转载于:https://www.cnblogs.com/Mychael/p/8974726.html

http://www.lryc.cn/news/2414650.html

相关文章:

  • 多质量动态系统仿真(Matlab代码实现)
  • 推荐文章:领略东方Project新境界 —— thprac,你的弹幕射击游戏进阶之路
  • 以太坊的单位wei是什么?
  • Winrar通用注册码
  • cellpadding跟cesllspacing区别有什么(table篇)
  • 如何学习数据库
  • CTF比赛总结(小白必看)
  • DWR基本原理及其流程
  • 格兰杰因果分析
  • 文件的简单写入
  • kail-信息收集之DNS解析(dnsenum、dnsrecon、dnswalk)
  • free mobile sex java_Mysql-从原理到优化,深入浅出数据库索引
  • VC2010使用过程常见问题
  • java的编程思想是什么'_编程思想 是什么
  • 栅栏密码(Fence crypto)
  • 监控公司局域网电脑的软件|局域网电脑监控软件哪个好用
  • 61850通讯流程
  • python中dumps、dump、load、loads的区别
  • 安装 USB-Blaster 驱动程序
  • Fire Game (连通块+两点bfs)
  • sobel算子实现原理和c++实现sobel()检测边缘函数
  • IO流原理及流的分类FileInputStream类和FileOutputStream类基本介绍
  • 一些使用频率高的指令及库版本安装切换
  • android——Spinner下拉列表案例详解(2),程序员真的是吃青春饭吗
  • PeopleSoft集成EasyExcel
  • 高级网络安全管理员 - 网络设备和安全配置:SSH 配置
  • XAPOFX1_5.dll文件丢失导致程序无法运行问题
  • Go切片(slices)
  • QT_快速入门
  • 深度linux 官网,深度OS