Magic boy Bi Luo with his excited tree
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1737 Accepted Submission(s): 521
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?
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.
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
题意:每个节点有一个价值Vi,每走一次一条边要花费Ci,问从各个节点出发最多能收获多少价值
题解:
不会做,照抄了大佬的代码,看懂了。
设g[x][]为包含x及x的子树的最大价值,f[x][]为包含x的父亲及除了x及x子树外其其他点的最大价值。1表示在某个点停下,0表示回到出发点。
则g[x][0] += max(0, g[son][0] - 2 * cost);
g[x][1] = max(g[x][1], max(g[x][0] + g[v][1] - cost, g[x][1] + g[v][0] - 2 * cost));
对于f数组,f[x][0] = max(f[x][0], f[fa][0] + g[fa][0] + tt - 2 * cost);
f[x][1] = max(f[x][1], max(f[fa][1] + g[fa][0] + tt - cost, f[fa][0] + g[fa][1] - cost));
当g[x][0] 大于等于2 * cost时,tt = - g[x][0] + 2 * cost; 否则tt = 0;
因为g[fa][1]可能会有经历x到x的子树,所以需要重新计算不经过x的最大的g[fa][1]。
综上
#include <stdio.h>
#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
const int maxn = 2e5 + 10;
int val[maxn], g1[maxn], far[maxn];
int g[maxn][2], f[maxn][2];
struct node{
int id, num;
node(int i, int n):id(i), num(n){}
node(){}
};
vector<node> s[maxn];
void dfs1(int x, int fa){
far[x] = fa;
int len = s[x].size();
g[x][0] = g[x][1] = val[x];
for(int i = 0; i < len; i++){
int v = s[x][i].id;
if(v == fa)
continue;
dfs1(v, x);
int temp1 = max(g[x][1], g[x][1] + g[v][0] - 2 * s[x][i].num);
int temp2 = g[x][0] + g[v][1] - s[x][i].num;
g[x][1] = temp1;
if(temp2 > temp1){
g[x][1] = temp2;
g1[x] = v;
}
g[x][0] = max(g[x][0], g[x][0] + g[v][0] - 2 * s[x][i].num);
}
}
void dfs2(int x, int fa, int cost){
f[x][0] = f[x][1] = 0;
int len = s[x].size();
if(g[x][0] >= 2 * cost){
f[x][0] = max(f[x][0], g[fa][0] + f[fa][0] - g[x][0]);
f[x][1] = max(f[x][1], f[fa][1] + g[fa][0] - g[x][0] + cost);
}
else{
f[x][0] = max(f[x][0], g[fa][0] + f[fa][0] - 2 * cost);
f[x][1] = max(f[x][1], f[fa][1] + g[fa][0] - cost);
}
if(g1[fa] == x){
int c1, c2;
c1 = c2 = val[fa];
int len1 = s[fa].size();
for(int i = 0; i < len1; i++){ //计算不包含x单链的g[fa][1]
int v = s[fa][i].id;
if(v == far[fa] || v == x)
continue;
c1 = max(c1, c1 + g[v][0] - 2 * s[fa][i].num);
c1 = max(c1, c2 + g[v][1] - s[fa][i].num);
c2 += max(0, g[v][0] - 2 * s[fa][i].num);
}
f[x][1] = max(f[x][1], f[fa][0] + c1 - cost);
}
else{
if(g[x][0] >= 2 * cost)
f[x][1] = max(f[x][1], g[fa][1] + f[fa][0] - g[x][0] + cost);
else
f[x][1] = max(f[x][1], g[fa][1] + f[fa][0] - cost);
}
for(int i = 0; i < len; i++){
int v = s[x][i].id;
if(v == fa)
continue;
dfs2(v, x, s[x][i].num);
}
}
int main(){
int t, n, a, b, c, cas = 1;
scanf("%d", &t);
while(t--){
printf("Case #%d:\n", cas++);
scanf("%d", &n);
for(int i = 1; i <= n; i++){
f[i][0] = f[i][1] = 0;
g1[i] = i;
scanf("%d", &val[i]);
}
for(int i = 1; i < n; i++){
scanf("%d %d %d", &a, &b, &c);
s[a].push_back(node(b, c));
s[b].push_back(node(a, c));
}
dfs1(1, 0);
dfs2(1, 0, 0);
for(int i = 1; i <= n; i++)
printf("%d\n", max(g[i][0] + f[i][1], g[i][1] + f[i][0]));
for(int i = 1; i <= n; i++)
s[i].clear();
}
return 0;
}