#include<bits/stdc++.h>
using namespace std;
#define i64 long long
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n), b(n), x(n);
for (int i = 1; i < n; i++) {cin >> a[i] >> b[i] >> x[i];}
vector<i64> d(n + 1, -1);
priority_queue<pair<i64,int>, vector<pair<i64, int>>,
greater<pair<i64,int>>>
pq;
pq.emplace(d[1] = 0, 1);
while(!pq.empty()) {
auto to = pq.top();
if(to.second == n) break;
pq.pop();
if(to.first != d[to.second]) continue;
if(d[to.second + 1] == - 1 || d[to.second + 1] > d[to.second] + a[to.second]) {
pq.emplace(d[to.second + 1] = d[to.second] + a[to.second], to.second + 1);
}
if(d[x[to.second]] == -1 || d[x[to.second]] > d[to.second] + b[to.second]) {
pq.emplace(d[x[to.second]] = d[to.second] + b[to.second], x[to.second]);
}
}
cout << d[n];
return 0;
}
abc340d
最新推荐文章于 2024-09-01 21:55:24 发布