2016中国大学生程序设计竞赛 - 网络选拔赛 C. Magic boy Bi Luo with his excited tree

Magic boy Bi Luo with his excited tree

Problem Description
Bi Luo is a magic boy, he also has a migic tree, the tree has N nodes , in each node , there is a treasure, it's value is V[i], and for each edge, there is a cost C[i], which means every time you pass the edge i , you need to pay C[i].

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?

 
Input
First line is a positive integer T(T≤104) , represents there are T test cases.

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.

 
Output
For the i-th test case , first output Case #i: in a single line , then output N lines , for the i-th line , output ans[i] in a single line.
 
Sample Input
1
5
4 1 7 7 7
1 2 6
1 3 1
2 4 8
3 5 2
 
Sample Output
Case #1:
15
10
14
9
15
 
Author
UESTC
 
Source
 
题意:
给出一棵树,每个点当经过它时有一个获益,每个点的获益只能计算一次,每条边当经过它都有一个花费,需要重复计算。
询问从每个点出发,随意走的最大收益。
题解:
树形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 ;
}
上一篇:Oracle 基于 RMAN 的不完全恢复(incomplete recovery by RMAN)


下一篇:02 - 用wxStreamToTextRedirector和wxTextCtrl输出std::cout