题意:
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2256
中文题。
思路:
一遍dfs,维护路径中遇到的传送门的最短时间即可。
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 10;
const LL INF = 1e15 + 10;
struct node {
LL v, w;
};
LL dp[MAXN], d[MAXN];
vector <node> tree[MAXN];
void dfs(int u, LL Mintime) {
int cnt = tree[u].size();
for (int i = 0; i < cnt; i++) {
LL v = tree[u][i].v, w = tree[u][i].w;
dp[v] = min(Mintime, dp[u] + w);
dfs(v, min(dp[v] + d[v], Mintime));
}
}
int main() {
//freopen("in.txt", "r", stdin);
int n;
while (scanf("%d", &n) != EOF) {
for (int i = 1; i <= n; i++) {
tree[i].clear();
scanf("%lld", &d[i]);
}
for (int i = 1; i < n; i++) {
LL p, w;
scanf("%lld%lld", &p, &w);
tree[p].push_back((node){i + 1, w});
}
dp[1] = 0;
dfs(1, d[1]);
for (int i = 1; i <= n; i++)
printf("%lld ", dp[i]);
puts("");
}
return 0;
}