Submission #705979

# Submission time Handle Problem Language Result Execution time Memory
705979 2023-03-05T17:48:25 Z mychecksedad Harbingers (CEOI09_harbingers) C++17
0 / 100
1000 ms 32912 KB
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define PI 3.1415926535
#define pb push_back
#define all(x) x.begin(), x.end()
const int N = 1e5+100, M = 1e5+10, K = 20;

struct Edge{
  int to, w;
};  

struct Line{
  bool type = false;
  float x = 0;
  ll m, c;
  bool operator < (const Line &other) const{
    if(type != other.type) return x < other.x;
    return m < other.m;
  }
};  


int n, dist[N];
vector<Edge> g[N];
ll s[N], c[N], dp[N];
deque<Line> cht;

float intersection(const Line &a, const Line &b){
  return (float) (a.c - b.c) / (b.m - a.m);
}



void pre(int v, int p){
  for(auto U: g[v]){
    int u = U.to;
    ll w = U.w;
    if(u != p){
      dist[u] = dist[v] + w;
      pre(u, v);
    }
  }
}

void dfs(int v, int p){
  // cout << v << '\n';
  // for(auto k: cht) cout << k.c << ' ' << k.m << ' ' << k.x << '\n';
  // cout << '\n';
  for(auto U: g[v]){
    int u = U.to;
    if(u != p){
      vector<Line> popp;

      Line j = *prev(upper_bound(all(cht), Line{1, (float) c[v], 0, 0}));
      dp[u] = j.c + j.m * c[u] + s[u] + c[u] * dist[u];

      Line L{0, 0, -dist[u], dp[u]};

      while(cht.size() > 1 && intersection(cht[cht.size() - 2], cht.back()) >= intersection(cht.back(), L)){
        popp.pb(cht.back());
        cht.pop_back();
      }
      cht.pb(L);
      if(cht.size() > 1)
        cht.back().x = intersection(cht[cht.size() - 2], cht.back());

      dfs(u, v);

      cht.pop_back();

      reverse(all(popp));
      for(auto &popped: popp) cht.pb(popped);
    }
  }
}

void solve(){
  cin >> n;
  for(int i = 0; i < n - 1; ++i){
    int u, v, w; cin >> u >> v >> w;
    g[u].pb({v, w});
    g[v].pb({u, w});
  }
  for(int i = 2; i <= n; ++i) cin >> s[i] >> c[i];

  dist[1] = 0;
  pre(1, 0);

  dp[1] = 0;
  cht.pb({0, 0, 0, 0});
  dfs(1, 0);  
  for(int i = 2; i <= n; ++i) cout << dp[i] << ' ';
}


int main(){
  cin.tie(0); ios::sync_with_stdio(0);
  int T = 1, aa;
  // cin >> T;aa=T;
  while(T--){
    // cout << "Case #" << aa-T << ": ";
    solve();
    cout << '\n';
  }
  return 0;
 
}

Compilation message

harbingers.cpp: In function 'int main()':
harbingers.cpp:102:14: warning: unused variable 'aa' [-Wunused-variable]
  102 |   int T = 1, aa;
      |              ^~
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2644 KB Output isn't correct
2 Incorrect 5 ms 3412 KB Output isn't correct
3 Incorrect 41 ms 14952 KB Output isn't correct
4 Incorrect 73 ms 20856 KB Output isn't correct
5 Incorrect 85 ms 27136 KB Output isn't correct
6 Runtime error 106 ms 32912 KB Memory limit exceeded
7 Incorrect 54 ms 13880 KB Output isn't correct
8 Execution timed out 1099 ms 19436 KB Time limit exceeded
9 Incorrect 386 ms 25068 KB Output isn't correct
10 Incorrect 521 ms 22684 KB Output isn't correct