Submission #641166

# Submission time Handle Problem Language Result Execution time Memory
641166 2022-09-16T06:42:14 Z sumit_kk10 Harbingers (CEOI09_harbingers) C++17
20 / 100
117 ms 37436 KB
#include<bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define F first
#define S second
#define int long long
using namespace std;
const int N = 1e6 + 5, MOD = 1e9 + 7;
vector<pair<int, int> > g[N];
int n, par[N], dis[N], dp[N], s[N], d[N];
bool vis[N];
struct Line{
    int m, c;

    int val(int x){
        return m*x + c;
    }
};

deque<Line> hulls;

bool check(Line a, Line b, Line cc){
    int x = (b.c - a.c);
    x = x * (a.m - cc.m);
    int y = (cc.c - a.c);
    y = y * (a.m - b.m);
    if(x > y)
        return true;
    return false;
}

void insert(int mm, int cc){
    Line cur;
    cur.m = mm;
    cur.c = cc;
    while(hulls.size() > 1){
        if(check(hulls[1], hulls[0], cur))
            hulls.pop_front();
        else
            break;
    }
    hulls.push_front(cur);
}

int get(int x){
    if(hulls.empty())
        return 0;
    int low = 1, high = (int) hulls.size() - 1, ans = 0;
    while(low <= high){
        int mid = (low + high) / 2;
        if(hulls[mid].val(x) >= hulls[mid - 1].val(x)){
            ans = mid;
            low = mid + 1;
        }
        else
            high = mid - 1;
    }
    return hulls[ans].val(x);
}

void dfs(int node, int p, int sum){
    dis[node] = sum;
    par[node] = p;
    for(auto k : g[node]){
        if(k.F == p) continue;
        dfs(k.F, node, sum + k.S);
    }
}

void bfs(int node){
    queue<int> q;
    q.push(node);
    vis[node] = true;
    while(!q.empty()){
        int node = q.front();
        q.pop();
        if(node != 1 and n <= 2500){
            // calculate dp[node]
            int cur = par[node], mn = 1e15;
            while(cur != 0){
                mn = min(mn, dp[cur] - dis[cur] * d[node]);
                cur = par[cur];
            }
            dp[node] = mn + s[node] + dis[node] * d[node];
        }
        else if(node != 1){
            int mn = s[node] + dis[node] * d[node];
            mn = min(mn, get(-d[node]) + s[node] + dis[node] * d[node]);
            dp[node] = mn;
            insert(dis[node], dp[node]);
        }
        for(auto k : g[node]){
            if(!vis[k.F]){
                vis[k.F] = true;
                q.push({k.F});
            }
        }
    }
}

void solve(){
    cin >> n;
    for(int i = 1; i < n; ++i){
        int u, v, c;
        cin >> u >> v >> c;
        g[u].pb({v, c});
        g[v].pb({u, c});
    }
    for(int i = 1; i < n; ++i)
        cin >> s[i + 1] >> d[i + 1];
    dfs(1, 0, 0);
    bfs(1);
    for(int i = 2; i <= n; ++i)
        cout << dp[i] << " ";
}

int32_t main(){
    fast;
    int t = 1;
    // cin >> t;
    while(t--)
        solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 27 ms 24168 KB Output is correct
3 Incorrect 53 ms 29520 KB Output isn't correct
4 Incorrect 83 ms 32252 KB Output isn't correct
5 Runtime error 106 ms 35008 KB Memory limit exceeded
6 Runtime error 117 ms 37436 KB Memory limit exceeded
7 Incorrect 69 ms 32244 KB Output isn't correct
8 Runtime error 110 ms 36380 KB Memory limit exceeded
9 Runtime error 110 ms 36628 KB Memory limit exceeded
10 Runtime error 86 ms 36000 KB Memory limit exceeded