2020Nowcode多校 Round9 B.Groundhog and Apple Tree
题意
给一棵树 初始\(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的更多相关文章
- POJ 2486 Apple Tree
好抽象的树形DP......... Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6411 Accepte ...
- poj 3321:Apple Tree(树状数组,提高题)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18623 Accepted: 5629 Descr ...
- poj 3321 Apple Tree dfs序+线段树
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Description There is an apple tree outsid ...
- [poj3321]Apple Tree(dfs序+树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26762 Accepted: 7947 Descr ...
- POJ 3321 Apple Tree(树状数组)
Apple Tree Time Limit: 2000MS Memory Lim ...
- 【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 ...
- POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25904 Accepted: 7682 Descr ...
- 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 ...
- POJ3321 Apple Tree (树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16180 Accepted: 4836 Descr ...
随机推荐
- js的函数-function
function函数 function的英文是[功能],[数] 函数:职责:盛大的集会的意思 在js里,就是函数的意思.在Java里叫做方法. 定义函数 function fun(参数){ //函数体 ...
- 十五:SQL注入之oracle,Mangodb注入
Access,Mysql,mssql,mangoDB,postgresql,sqlite,oracle,sybase JSON类型的数据注入: 键名:键值 {"a":"1 ...
- 虚拟机linux共享文件夹
linux共享文件夹问题 1. 初配置 2. 挂载 018.7.8 为了实现win7与VMware中linux文件夹共享很花费了一些时间,但终成正果 1. 初配置 虚拟机设置->选项->共 ...
- C语言变量
C语言变量 任何一种编程语言都离不开变量,特别是数据处理型程序,变量的使用非常频繁,没有变量参与程序甚至无法编制,即使编制运行后的意义也不大.变量之所以重要,是因为变量是编程语言中数据的符号标识和载体 ...
- kafka安装流程
本文是作者原创,版权归作者所有.若要转载,请注明出处. 安装前的环境准备 1.由于Kafka是用Scala语言开发的,运行在JVM上,在安装之前需要先安装JDK(省略) 2.kafka依赖zookee ...
- C++:标准I/O流
标准I/O对象:cin,cout,cerr,clog cout; //全局流对象 输出数据到显示器 cin; //cerr没有缓冲区 clog有缓冲区 cerr; //标准错误 输出数据到显示器 cl ...
- SDNU_ACM_ICPC_2021_Winter_Practice_1st [个人赛] 2021.1.19 星期二
SDNU_ACM_ICPC_2021_Winter_Practice_1st [个人赛] K - Color the ball 题意: 有n个气球,每次都给定两个整数a,b,给a到b内所有的气球涂一个 ...
- 入门OJ:扫雪
扫雪1 题目描述 大雪履盖了整个城市,市政府要求冬季服务部门尽快将一些街道(列在一份清单中)的积雪清除掉以恢复交通,整个城市由许多交叉路口和街道构成,当然任意两个交叉路口都是直接或间接连通的,清单给出 ...
- ECharts特效-折线图上一个光点来回移动
参考地址:https://www.makeapie.com/editor.html?c=xxxcG6NGk0 效果图打开上面的网址就可以看到. 核心代码如下,该效果实现的难点是数据的封装,还有eval ...
- Servlet中的一些注意事项
servlet中的一些注意事项 1 什么是servlet? 1)Servlet是Sun公司制定的一套技术标准,包含与Web应用相关的一系列接口,是Web应用实现方式的宏观解决方案.而具体的Servle ...