#include <bits/stdc++.h>
#define st first
#define nd second
using lint = int64_t;
constexpr int mod = int(1e9) + 7;
constexpr int inf = 0x3f3f3f3f;
constexpr int ninf = 0xcfcfcfcf;
constexpr lint linf = 0x3f3f3f3f3f3f3f3f;
const long double pi = acosl(-1.0);
// Returns -1 if a < b, 0 if a = b and 1 if a > b.
int cmp_double(double a, double b = 0, double eps = 1e-9) {
return a + eps > b ? b + eps > a ? 0 : 1 : -1;
}
using namespace std;
struct Line{
mutable lint k, m, p;
bool operator<(const Line& o) const { return k < o.k; }
bool operator<(lint x) const {return p < x; }
};
struct LineContainer : multiset<Line, less<>>{
static const lint inf = LLONG_MAX;
lint div(lint a, lint b){
return a / b - ((a ^ b) < 0 && a % b);
}
bool isect(iterator x, iterator y){
if(y == end()){ x->p = inf; return false; }
if(x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(lint k, lint m){
auto z = insert({k, m, 0}), y = z++, x = y;
while(isect(y, z)) z = erase(z);
if(x != begin() && isect(--x, y)) isect(x, y = erase(y));
while((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
lint query(lint x){
assert(!empty());
auto l = *lower_bound(x);
return l.k * x + l.m;
}
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin>>n;
vector<vector<pair<int, lint>>>g(n);
for(int i=1;i<n;i++){
int a, b, c;
cin>>a>>b>>c;
a--, b--;
g[a].push_back({b, c});
g[b].push_back({a, b});
}
vector<lint>s(n), v(n);
for(int i=1;i<n;i++) cin>>s[i]>>v[i];
int sz = sqrt(n * log(n));
vector<lint>dist(n), dp(n, linf);
dp[0] = 0;
vector<int>par(n);
vector<LineContainer>st;
auto dfs = [&](auto& self, int u, int p, int d)->void{
par[u] = p;
if(d && d % sz == 0){
st.push_back(LineContainer());
LineContainer& cht = st.back();
for(int i=0,x=par[u];i<sz;i++,x=par[x]){
cht.add(dist[x], -dp[x]);
}
}
{
// calculate ans
lint cnt = dist[u] * v[u] + s[u];
dp[u] = cnt;
for(int i=0,x=par[u];i<d%sz;i++,x=par[x]){
dp[u] = min(dp[u], cnt - dist[x] * v[u] + dp[x]);
}
for(auto& cht : st){
lint res = cht.query(v[u]);
dp[u] = min(dp[u], cnt - cht.query(v[u]));
}
}
for(auto [nxt, c] : g[u]){
if(nxt == p) continue;
dist[nxt] = dist[u] + c;
self(self, nxt, u, d+1);
}
if(d && d % sz == 0) st.pop_back();
};
dfs(dfs, 0, 0, 0);
for(int i=1;i<n;i++) cout<<dp[i]<<" ";
cout<<"\n";
return 0;
}
/*
[ ]Leu o problema certo???
[ ]Ver se precisa de long long
[ ]Viu o limite dos fors (é n? é m?)
[ ]Tamanho do vetor, será que é 2e5 em vez de 1e5??
[ ]Testar sample
[ ]Testar casos de borda
[ ]1LL no 1LL << i
[ ]Testar mod (é 1e9+7, mesmo?, será que o mod não ficou negativo?)
*/
Compilation message
harbingers.cpp: In instantiation of 'main()::<lambda(auto:23&, int, int, int)> [with auto:23 = main()::<lambda(auto:23&, int, int, int)>]':
harbingers.cpp:95:18: required from here
harbingers.cpp:84:10: warning: unused variable 'res' [-Wunused-variable]
84 | lint res = cht.query(v[u]);
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Incorrect |
3 ms |
1104 KB |
Output isn't correct |
3 |
Incorrect |
163 ms |
13980 KB |
Output isn't correct |
4 |
Incorrect |
295 ms |
20716 KB |
Output isn't correct |
5 |
Incorrect |
455 ms |
26268 KB |
Output isn't correct |
6 |
Incorrect |
639 ms |
32220 KB |
Output isn't correct |
7 |
Incorrect |
295 ms |
14924 KB |
Output isn't correct |
8 |
Incorrect |
695 ms |
24640 KB |
Output isn't correct |
9 |
Incorrect |
909 ms |
28668 KB |
Output isn't correct |
10 |
Incorrect |
784 ms |
26100 KB |
Output isn't correct |