题意

给一棵树 初始\(hp=0\) 经过一条边会掉血\(w_{i}\) 第一次到达一个点可以回血\(a_{i}\) 在一个点休息\(1s\)可以回复\(1hp\) 血不能小于\(0\)

每条边最多经过两次 求从起点经过所有点再回到起点到最小时间

题解

休息的话干脆就在起点休息够了再出发吧

在分叉路口的时候走哪个方向是个问题

对于每一棵子树 可以维护出走这个方向的最小\(hp\) 和走回来还剩下的\(hp\)

那么可以根据这两个值的大小关系把子树分成两种 一种是走回来回的血比走之前掉的血还多 还有一种...

显然可以在第一种的选一个掉血最少的开始先走 走完之后还剩一些血

对于剩下的 比较一下谁先走更优

#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <set>
#include <string.h>
#include <string>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
const int mod = 998244353;
const int INF = 0x3f3f3f3f;
const int MAXN = 100005; int n, cnt;
int a[MAXN];
struct node {
int to, nex, val;
}E[MAXN << 1];
int head[MAXN]; struct ed {
int id;
ll cost, gain;
}dp[MAXN]; bool cmp(ed A, ed B) {
if(A.gain > A.cost) {
if(B.gain > B.cost) return A.cost < B.cost;
else return true;
} else {
if(B.gain > B.cost) return false;
else return A.gain > B.gain;
}
} void dfs(int x, int fa) {
vector<ed> comp;
for(int i = head[x]; i; i = E[i].nex) {
int v = E[i].to;
if(v == fa) continue; dfs(v, x);
dp[v].id = v;
dp[v].cost += E[i].val;
dp[v].gain -= E[i].val;
if(dp[v].gain < 0) dp[v].cost -= dp[v].gain, dp[v].gain = 0; comp.push_back(dp[v]);
}
sort(comp.begin(), comp.end(), cmp); dp[x].cost = 0;
ll now = a[x];
for(int i = 0; i < comp.size(); i++) { ed v = comp[i];
now -= v.cost;
if(now < 0) {
dp[x].cost += -now;
now = 0;
}
now += v.gain;
}
dp[x].gain = now;
} int main() {
int T;
cin>>T;
while(T--) {
cnt = 0;
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]), head[i] = 0; for(int i = 1; i < n; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
E[++cnt].to = v; E[cnt].nex = head[u]; head[u] = cnt; E[cnt].val = w;
E[++cnt].to = u; E[cnt].nex = head[v]; head[v] = cnt; E[cnt].val = w;
}
dfs(1, 0);
printf("%lld\n", dp[1].cost);
}
return 0;
}

2020Nowcode多校 Round9 B.Groundhog and Apple Tree的更多相关文章

  1. POJ 2486 Apple Tree

    好抽象的树形DP......... Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6411 Accepte ...

  2. poj 3321:Apple Tree(树状数组,提高题)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 5629 Descr ...

  3. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  4. [poj3321]Apple Tree(dfs序+树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26762   Accepted: 7947 Descr ...

  5. POJ 3321 Apple Tree(树状数组)

                                                              Apple Tree Time Limit: 2000MS   Memory Lim ...

  6. 【HDU 4925】BUPT 2015 newbie practice #2 div2-C-HDU 4925 Apple Tree

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/C Description I’ve bought an or ...

  7. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

  8. URAL 1018 Binary Apple Tree(树DP)

    Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a bina ...

  9. POJ3321 Apple Tree (树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16180   Accepted: 4836 Descr ...

随机推荐

  1. 【JavaWeb】现代 JavaScript 教程

    js_model_tutorial !!待更新 前言 现代 JavaScript 教程的学习笔记,它是一份不错的学习资源,感谢开源. 中文链接 基础 函数 代码示例 函数的声明方式 function ...

  2. CopyOnWriteArrayList设计思路与源码分析

    CopyOnWriteArrayList实现了List接口,RandomAccess,Cloneable,Serializable接口. CopyOnWriteArrayList特性 1.线程安全,在 ...

  3. Zabbix 4.0.24 完整安装

    依赖包安装: yum install net-snmp* libssh-devel libssh2-devel -y Zabbix server安装: wget https://cdn.zabbix. ...

  4. 无限重置IDE过期时间插件 亲测可以使用

    相信破解过IDEA的小伙伴,都知道jetbrains-agent这个工具,没错,就是那个直接拖入到开发工具界面,一键搞定,so easy的破解工具!这个工具目前已经停止更新了,尽管还有很多小伙伴在使用 ...

  5. 温故而知新--day2

    温故而知新--day2 类 类与对象 类是一个抽象的概念,是指对现实生活中一类具有共同特征的事物的抽象.其实列化后称为对象.类里面由类属性组成,类属性可以分为数据属性和函数属性(函数属性又称为类方法) ...

  6. LeetCode349. 两个数组的交集

    题目 给定两个数组,编写一个函数来计算它们的交集. 分析 数组元素值可以很大,所以不适合直接开数组进行哈希,这里要学习另一种哈希方式:集合 集合有三种,区别见下面代码随想录的Carl大佬的表格,总结的 ...

  7. 使用line_profiler对python代码性能进行评估优化

    性能测试的意义 在做完一个python项目之后,我们经常要考虑对软件的性能进行优化.那么我们需要一个软件优化的思路,首先我们需要明确软件本身代码以及函数的瓶颈,最理想的情况就是有这样一个工具,能够将一 ...

  8. java 记录数据持续变化时间

    1.需求:获取count为null和不为null的持续变化 [{count=0, time=0}, {count=10, time=1000}, {count=20, time=2000}, {cou ...

  9. 与图论的邂逅06:dfs找环

    当我在准备做基环树的题时,经常有了正解的思路确发现不会找环,,,,,,因为我实在太蒻了. 所以我准备梳理一下找环的方法: 有向图 先维护一个栈,把遍历到的节点一个个地入栈.当我们从一个节点x回溯时无非 ...

  10. mysql InnoDB架构

    1.InnoDB的磁盘结构 1)系统表空间 2)用户表空间 3)rodolog 文件组 4)磁盘文件逻辑结构 文件->段->区->页->行 InnoDB对数据的存取是以页为单位 ...