HDU
来源:互联网 发布:天谕流光女捏脸数据 编辑:程序博客网 时间:2024/06/08 06:57
树形dp
结构体数组存两点之间关系
dp [ i ] 表示以 i 结点为结尾时 最大的盈利值,进行更新后遍历dp 数组找最大的 answer
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <cmath>#include <set>#include <map>#include <stack>#include <queue>#include <ctype.h>#include <vector>#include <algorithm>#include <sstream>#define PI acos(-1.0)#define in freopen("in.txt", "r", stdin)#define out freopen("out.txt", "w", stdout)using namespace std;typedef long long ll;const int maxn = 2e5 + 7, INF = 0x3f3f3f3f, mod = 1e9 + 7;int T, n, pos, a[maxn], dp[maxn];struct node { int x, y, cost;}g[maxn];void init() { pos = 0; for(int i = 1; i <= n; ++i) scanf("%d", &a[i]); int u, v, co; for(int i = 1; i < n; ++i) { scanf("%d%d%d", &u, &v, &co); g[pos].x = u, g[pos].y = v, g[pos].cost = co;pos++; g[pos].x = v, g[pos].y = u, g[pos].cost = co;pos++; }}int main() { scanf("%d", &T); while(T--) { scanf("%d", &n); init(); memset(dp, 0, sizeof dp); for(int i = 0; i < pos; ++i) { dp[g[i].y] = max(dp[g[i].y], dp[g[i].x] - a[g[i].x] - g[i].cost + a[g[i].y]); } int ans = 0; for(int i = 0; i < pos; ++i) ans = max(ans, dp[i]); cout << ans << endl; } return 0;}
阅读全文
1 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- Matlab中fminunc函数的意义 以及options函数的初级用法
- linux配置网卡后无法开机
- QTreeWidget 填充Indictor指示器背景色
- S3C2440之IIC
- rhel6.5 环境nginx安装
- HDU
- shell正则表达式
- 215. Kth Largest Element in an Array(数组中第k大元素)
- JavaScript基本概念(四)
- vue-devtools在Chrom浏览器上的安装
- 优化 Vue 项目编译文件大小
- 录入学员信息
- 【springboot 入门篇】第0篇 spring-boot详细解释
- 我的第一篇博客--成长的第一站