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 ...
随机推荐
- Oracle误删数据的恢复
Oracle误删数据的恢复,分为两种方法:SCN和时间戳两种方法恢复. 一.通过SCN恢复删除且已提交的数据 1.获得当前数据库的SCN号 select current_scn from v$data ...
- oracle 11g 安装与卸载
安装 点击是,这是位数不一致,但可用. 桌面类――这种安装方式一般适用于台式机和笔记本.它包含一个最小数据库和最低的配置需求. 服务器类――这种安装方式适用于服务器,例如,它会向您提供数据中心和用于支 ...
- 【Nginx】配置nginx图片服务器
想通过nginx来访问服务器上的图片 可以搭建一个nginx图片服务器. 做法如下: 先安装nginx,这里直接用yum来进行安装的 安装方法如下: https://blog.csdn.net/iml ...
- 【MySQL】ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing
今天上午遇到了一个问题,新创建的mysql5.7的数据库,由于初始化有点问题,没有给root密码,用了免密码登录. 但是,修改了root密码之后,把配置中的免密登录的配置注释掉后,重启服务.服务正常启 ...
- DB2版本升级(V9.7升级到V11.1)
1.V11.1版本升级路线 DB2 11.1 可以将现有的 Db2 V9.7.Db2 V10.1 或 Db2 V10.5 实例和数据库直接升级到 Db2 V11.1.如果 Db2 服务器正在 Db2 ...
- HTML&CSS:构建网站不能不说的那些事儿
很高兴你能看到这个专栏!俗话说得好,相逢即是缘分,没准你和我在上一世也曾有过五百次的回眸,才得此一面.说的有点恶心了,咱还是书归正传,说说这个专栏吧. 这个专栏主要讲的是 HTML 和 CSS 的页面 ...
- TCP三次握手Linux源码解析
TCP是面向连接的协议.面向连接的传输层协议在原点和重点之间建立了一条虚拟路径,同属于一个报文的所有报文段都沿着这条虚拟路径发送,为整个报文使用一条虚拟路径能够更容易地实施确认过程以及对损伤或者丢失报 ...
- js千分位分隔,数字货币化方法学习记录
js千分位分隔,数字货币化-4种方法(含正则) 方法1-整数货币化 // 整数货币化 function intCurrency(num) { var reg = new RegExp("^[ ...
- 【python刷题】LRU
什么是LRU? LRU是Least Recently Used的缩写,即最近最少使用,是一种常用的页面置换算法,选择最近最久未使用的页面予以淘汰.该算法赋予每个页面一个访问字段,用来记录一个页面自上次 ...
- Beating JSON performance with Protobuf https://auth0.com/blog/beating-json-performance-with-protobuf/
Beating JSON performance with Protobuf https://auth0.com/blog/beating-json-performance-with-protobuf ...