(1)代码 + 思路(注释)
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
#define ll long long
vector<pair<int, int>> resu[N];
int n;
int und[N];
int maxTime;
void dfs(int x, int tmp) {
maxTime = max(maxTime, tmp + und[x]);
for (int i = 0; i < resu[x].size(); ++i) {
dfs(resu[x][i].first, tmp + resu[x][i].second);
}
}
int main() {
cin >> n;
for (int i = 2; i <= n; ++i) {
cin >> und[i];
}
for (int i = 2; i <= n; ++i) {
int x, y, k;
cin >> x >> y >> k;
resu[x].push_back({y, k});
}
dfs(1, 0);
cout << maxTime << endl;
return 0;
}
(2)测试数据
3
3 4
1 2 4
1 3 2
|