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 ...
随机推荐
- VRP CommandLines
<> 用户视图 通过 system-view 进入系统视图 [] 系统视图 通过interface 0/0/0 进入接口视图 CTRL+Z 返回用户视图 CTRL+A 把光标移动到当前命令 ...
- tf.argmax(vector,axis)函数的使用
1.返回值 vector为向量,返回行或列的最大值的索引号: vector为矩阵,返回值是向量,返回每行或每列的最大值的索引号. 2.参数 vector为向量或者矩阵 axis = 0 或1 0:返回 ...
- golang遍历时修改被遍历对象
目录 前言 遍历切片 遍历map 总结 前言 很多时候需要将遍历对象中去掉某些元素,或者往遍历对象中添加元素,这时候就需要小心操作了. 对于go语言中的一些注意事项我做了总结和示例,留下点笔记. 遍历 ...
- TCP/IP五层模型-传输层-UDP协议
1.定义:UDP:是非面向连接.不可靠的用户数据包协议. 2.应用场景:适合对数据完整性要求不高,但对延迟很敏感,比如即时通信(语音视频聊天等). 3.UDP报文格式: 4.用UDP传输数据的应用层 ...
- mysql中更改字段属性实际上都做了哪些操作
mysql> set profiling=1; Query OK, 0 rows affected (0.00 sec) mysql> alter table test modify n ...
- Golang应用性能问题排查分析
背景 公司有一个使用golang开发的采集模块,负责调用多个外部系统采集数据:最近做了一次架构上的调整,将采集模块分成api.job两个子模块,并部署到容器中,拆分前部署在虚机上. 现象 部分采集任务 ...
- 利用vbs隐藏dos窗口
方法一: option explicitdim wshshellset wshshell=wscript.createobject("wscript.shell")wshshell ...
- [Usaco2005 Dec]Scales 天平
题目描述 约翰有一架用来称牛的体重的天平.与之配套的是N(1≤N≤1000)个已知质量的砝码(所有砝码质量的数值都在31位二进制内).每次称牛时,他都把某头奶牛安置在天平的某一边,然后往天平另一边加砝 ...
- Development desciptor
概述与作用: 部署描述符是用于描述Web应用程序的元数据,并为Java EE Web应用程序服务器部署和运行Web应用程序提供指令.从传统上来说,所有元数据都来自于部署描述符文件/WEB-INF/we ...
- 如何在 crontab 中让 source ~/.bashrc 生效
cron 是许多类 Unix 操作系统中都自带的用来调度定时任务的工具,定时任务的配置是写在 crontab 文件中的,但是 crontab 文件不允许直接编辑,一般都是通过命令 crontab -e ...