QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#29219#2161. The Cost of Speed LimitsHakujitsuWA 3ms4464kbC++172.5kb2022-04-15 20:52:082022-04-28 14:12:17

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-04-28 14:12:17]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:4464kb
  • [2022-04-15 20:52:08]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

void debug_out() {cerr << endl;}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T)
{
    cerr << " " << H;
    debug_out(T...);
}
#ifndef ONLINE_JUDGE
    #define debug(...) cerr << "{" << #__VA_ARGS__ << "}:", debug_out(__VA_ARGS__)
#else
    #define debug(...) 42
#endif

using ll = long long;
const int maxn = 2e4 + 233;
vector<pair<int, int> > G[maxn];
ll dp[20][maxn];
ll best[maxn];
ll tmp[maxn];

vector<tuple<int, int, int> > edge;

int sz[maxn], d[maxn], son[maxn];
int top;
int n, m, c;
vector<int> lst;
int value[maxn];

void dfs1(int u, int fa){
    d[u] = d[fa] + 1;
    sz[u] = 1;
    for(auto [v, w] : G[u]){
        if(v == fa) continue;
        dfs1(v, u);
        value[v] = w;
        sz[u] += sz[v];
        if(sz[v] > sz[son[u]]) son[u] = v;
    }
}

void dfs2(int u, int fa) {
    int id = top;
    if(son[u]) {
        dfs2(son[u], u);
        for (int i = 0; i <= m; i += 1) {
            tmp[i] = 1e18;
        }
        tmp[0] = best[son[u]] + c;
        for (int i = value[son[u]]; i <= m; i += 1) {
            tmp[i] = min(dp[top][i], dp[top][0] + c) + lst[i - 1] - lst[value[son[u]] - 1];
        }
        memcpy(dp[id], tmp, sizeof(tmp));
    }
    for (auto [v, w] : G[u]) {
        if(v == fa || v == son[u]) continue;
        top += 1;
        dfs2(v, u);
        for (int i = w; i <= m; i += 1) {
            dp[id][i] += min(dp[top][i] , dp[top][0] + c) + lst[i - 1] - lst[w - 1];
        }
        dp[id][0] += best[v] + c;
        top -= 1;
    }
    best[u] = dp[id][0] + c; 
    for (int i = value[u]; i <= m; i += 1) {
        best[u] = min(best[u], dp[id][i] + lst[i - 1] - lst[value[u] - 1]);
    }
}

int main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n >> c;
    for (int i = 1; i < n; i += 1){
        int u, v, w;
        cin >> u >> v >> w;
        edge.emplace_back(u, v, w);
        lst.emplace_back(w);
    }
    sort(lst.begin(), lst.end());
    lst.erase(unique(lst.begin(), lst.end()), lst.end());
    m = lst.size();
    for (auto [u, v, w] : edge) {
        int p = lower_bound(lst.begin(), lst.end(), w) - lst.begin() + 1;
        G[u].emplace_back(v, p);
        G[v].emplace_back(u, p);
    }
    value[1] = 1;
    dfs1(1, 0);
    dfs2(1, 0);
    ll res = 1e18;
    for (int i = 0; i <= m; i += 1) {
        res = min(res, dp[top][i]);
    }
    cout << res << "\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 4464kb

input:

13 20
1 8 101
1 9 30
1 2 100
1 3 100
2 4 75
2 5 70
2 6 82
2 7 77
3 10 73
3 11 69
3 12 83
3 13 79

output:

262

result:

wrong answer 1st lines differ - expected: '272', found: '262'