树形dp
结构体数组存两点之间关系
dp [ i ] 表示以 i 结点为结尾时 最大的盈利值,进行更新后遍历dp 数组找最大的 answer
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <ctype.h>
#include <vector>
#include <algorithm>
#include <sstream>
#define PI acos(-1.0)
#define in freopen("in.txt", "r", stdin)
#define out freopen("out.txt", "w", stdout)
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 7, INF = 0x3f3f3f3f, mod = 1e9 + 7;
int T, n, pos, a[maxn], dp[maxn];
struct node {
int x, y, cost;
}g[maxn];
void init() {
pos = 0;
for(int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
int u, v, co;
for(int i = 1; i < n; ++i) {
scanf("%d%d%d", &u, &v, &co);
g[pos].x = u, g[pos].y = v, g[pos].cost = co;pos++;
g[pos].x = v, g[pos].y = u, g[pos].cost = co;pos++;
}
}
int main() {
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
init();
memset(dp, 0, sizeof dp);
for(int i = 0; i < pos; ++i) {
dp[g[i].y] = max(dp[g[i].y], dp[g[i].x] - a[g[i].x] - g[i].cost + a[g[i].y]);
}
int ans = 0;
for(int i = 0; i < pos; ++i)
ans = max(ans, dp[i]);
cout << ans << endl;
}
return 0;
}