2016中国大学生程序设计竞赛 - 网络选拔赛 C. Magic boy Bi Luo with his excited tree
Magic boy Bi Luo with his excited tree
You may attention that every V[i] can be taken only once, but for some C[i] , you may cost severial times.
Now, Bi Luo define ans[i] as the most value can Bi Luo gets if Bi Luo starts at node i.
Bi Luo is also an excited boy, now he wants to know every ans[i], can you help him?
Four each test:
The first line contain an integer N(N≤105).
The next line contains N integers V[i], which means the treasure’s value of node i(1≤V[i]≤104).
For the next N−1 lines, each contains three integers u,v,c , which means node u and node v are connected by an edge, it's cost is c(1≤c≤104).
You can assume that the sum of N will not exceed 106.
5
4 1 7 7 7
1 2 6
1 3 1
2 4 8
3 5 2
15
10
14
9
15
题意:
给出一棵树,每个点当经过它时有一个获益,每个点的获益只能计算一次,每条边当经过它都有一个花费,需要重复计算。
询问从每个点出发,随意走的最大收益。
题解:
树形dp。
比赛时没有A掉。。。队友没有采取我的思路写,按照他们推的公式写了。。。
(其实是当时我在写其他题,我写完他们直接上了。。。实际上不用那么急,
(当时比赛结束还有1.5h,好好整理思路写说不定就过了
其实我觉得我的思路是比较简单明了的。。
今天写了一下,从整理题目思路在内到AC,只花了1h,一发就AC了。。。 设f[i]代表每个点出去随便走不一定回来的最大收益和次大收益(一对数),
个g[i]代表每个点出去随便走一定要回来的最大收益。 按照bfs序扫两遍。
第一遍,按照bfs序倒着扫:
统计每个点只能往下走的 f 和 g。
这部分是非常简单的dp,方程
g[i] = sigma(max(0, g[child] - 2 * costOfEdge))
f[i] = max(g[i],
g[i] - max(0, g[child] - 2 * costOfEdge) + f[child] + costOfEdge)
计算f时先将孩子对g[i]的贡献减去加上本次贡献就行了。 第二遍,按照bfs序正着扫:
统计每个点的f和g,加入父亲的影响。
也就是说此时的f和g的意义变为不仅仅可以往下走还可以往父亲边走,就是随便走。
统计时类似,先减去自己对父亲的影响,再利用父亲的f和g更新自己。
转移方程这里写的不方便,具体可以查看代码。 最后每个点的f值就是答案。
因为f值不会比g值小,这点可以从定义或者方程看出。
const int N = ;
int head[N], son[N * ], val[N * ], nex[N * ], tot;
int w[N], n;
struct FirstAndSecondMax {
pair<int, int> first, second; inline void init(int val, int id) {
first = second = make_pair(val, id);
} inline void addAll(int w) {
first.first += w, second.first += w;
} inline void updata(int val, int id) {
pair<int, int> now = make_pair(val, id);
if(first < now) swap(first, now);
if(second < now) swap(second, now);
} inline int getMax(int except) {
return first.second == except ? second.first : first.first;
}
} f[N];
int g[N];
// f -> maximum of paths that need not go back
// g -> maximum of paths that need to go back
#define cg(origin, cost) (max(0, origin - 2 * cost))
#define cf(origin, cost) (max(0, origin - cost))
// cg -> contribution to g
// cf -> contribution to f
int ans[N]; inline void init() {
for(int i = ; i < n; ++i) f[i].init(w[i], i);
for(int i = ; i < n; ++i) head[i] = -;
tot = ;
} inline void addEdge(int u, int v, int w) {
son[tot] = v, val[tot] = w, nex[tot] = head[u];
head[u] = tot++;
} int que[N], fa[N], valfa[N];
inline void bfs(int st) {
int len = ;
fa[st] = -, valfa[st] = INF, que[len++] = st;
for(int idx = ; idx < len; ++idx) {
int u = que[idx];
for(int tab = head[u], v; tab != -; tab = nex[tab])
if((v = son[tab]) != fa[u])
fa[v] = u, valfa[v] = val[tab], que[len++] = v;
}
} inline void solve() {
bfs(); for(int idx = n - ; idx >= ; --idx) {
int u = que[idx];
g[u] = w[u];
for(int tab = head[u], v; tab != -; tab = nex[tab])
if((v = son[tab]) != fa[u])
g[u] += cg(g[v], val[tab]);
f[u].init(g[u], u);
for(int tab = head[u], v; tab != -; tab = nex[tab])
if((v = son[tab]) != fa[u])
f[u].updata(g[u] - cg(g[v], val[tab]) +
f[v].getMax(u) - val[tab], v);
} for(int idx = ; idx < n; ++idx) {
int u = que[idx];
if(fa[u] != -) {
int faContributeIt = g[fa[u]] - cg(g[u], valfa[u]);
int lastg = g[u];
g[u] += cg(faContributeIt, valfa[u]);
f[u].addAll(cg(faContributeIt, valfa[u]));
f[u].updata(f[fa[u]].getMax(u) - cg(lastg, valfa[u])
- valfa[u] + lastg, fa[u]);
}
ans[u] = f[u].getMax(-);
} for(int i = ; i < n; ++i) printf("%d\n", ans[i]);
} int main() {
int testCase;
scanf("%d", &testCase);
for(int testIndex = ; testIndex <= testCase; ++testIndex) {
printf("Case #%d:\n", testIndex);
scanf("%d", &n);
for(int i = ; i < n; ++i) scanf("%d", &w[i]);
init();
for(int i = , u, v, w; i < n; ++i) {
scanf("%d%d%d", &u, &v, &w);
--u, --v;
addEdge(u, v, w), addEdge(v, u, w);
}
solve();
}
return ;
}
2016中国大学生程序设计竞赛 - 网络选拔赛 C. Magic boy Bi Luo with his excited tree的更多相关文章
- 2016中国大学生程序设计竞赛 - 网络选拔赛 J. Alice and Bob
Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- 2016中国大学生程序设计竞赛 网络选拔赛 I This world need more Zhu
This world need more Zhu Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- 2016中国大学生程序设计竞赛 - 网络选拔赛 1004 Danganronpa
Problem Description Chisa Yukizome works as a teacher in the school. She prepares many gifts, which ...
- 2016中国大学生程序设计竞赛 - 网络选拔赛 1011 Lweb and String
Problem Description Lweb has a string S. Oneday, he decided to transform this string to a new sequen ...
- 2016中国大学生程序设计竞赛 - 网络选拔赛 1001 A water problem (大数取余)
Problem Descripton Two planets named Haha and Xixi in the universe and they were created with the un ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ's Salesman 【离散化+树状数组维护区间最大值】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/O ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】
Tree and Permutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- HDU 6154 - CaoHaha's staff | 2017 中国大学生程序设计竞赛 - 网络选拔赛
/* HDU 6154 - CaoHaha's staff [ 构造,贪心 ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 题意: 整点图,每条线只能连每个方格的边或者对角线 问面积大于n的 ...
随机推荐
- PDO防sql注入原理分析
使用pdo的预处理方式可以避免sql注入. 在php手册中'PDO--预处理语句与存储过程'下的说明: 很多更成熟的数据库都支持预处理语句的概念.什么是预处理语句?可以把它看作是想要运行的 SQL 的 ...
- memcached的key,value,过期时间的限制
1. key值最大长度? memcached的key的最大长度是250个字符,是memcached服务端的限制. 如果您使用的客户端支持"key的前缀"或类似特性,那么key( ...
- 在Application中集成Microsoft Translator服务之翻译语言代码
Microsoft Translator支持多种语言,当我们获取服务时使用这些代码来表示我们是使用哪种语言翻译成什么语言,以下是相关语言对应的代码和中文名 为了方便我已经将数据库上传到云盘上,读者可 ...
- php 去掉字符串的最后一个字符
原字符串1,2,3,4,5,6, 去掉最后一个字符",",最终结果为1,2,3,4,5,6 代码如下: $str = "1,2,3,4,5,6,"; $news ...
- Dijkstra算法
Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止. /*图的邻接矩阵表示*/typedef ...
- Backbone源码阅读手记
Backbone.js是前端的MVC框架,它通过提供模型Models.集合Collection.视图Veiew赋予了Web应用程序分层结构.从源码中可以知道,Backbone主要分了以下几个模块: ( ...
- 【转载】使用pandas进行数据清洗
使用pandas进行数据清洗 本文转载自:蓝鲸的网站分析笔记 原文链接:使用python进行数据清洗 目录: 数据表中的重复值 duplicated() drop_duplicated() 数据表中的 ...
- vertx verticle
以下内容为随手记的,若看客不知鄙人所云,还请原谅则个.............. 公司用的vertx,在国内,这还是款比较年轻的框架,你也可以把他当做一个工具,官网上的说法是: Vert.x is a ...
- tyvj1172 自然数拆分Lunatic版
背景 话说小小鱼看了P1171(自然数拆分)之后感觉异常不爽,于是异常邪恶地将题目加强. 描述 输入自然数n,然后将其拆分成由若干数相加的形式,参与加法运算的数可以重复. 输入格式 输入只有一个整数n ...
- PhpStorm 8.x/9.x 快捷键设置/个性化设置,如何多项目共存?如何更换主题?
1."自定义"常用快捷键(设置成跟Eclipse差不多) 按照路径:File -> Settings -> Appearance & Behavior -> ...