QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#549714#2161. The Cost of Speed LimitsHKOI0ML 6ms14120kbC++201.8kb2024-09-06 20:04:032024-09-06 20:04:03

Judging History

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

  • [2024-09-06 20:04:03]
  • 评测
  • 测评结果:ML
  • 用时:6ms
  • 内存:14120kb
  • [2024-09-06 20:04:03]
  • 提交

answer

#include <bits/stdc++.h>
#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const ll inf = 0x3f3f3f3f3f3f3f3f;
const int MAX = (int)1e5 + 1;

void solve() {
    int n, c;
    cin >> n >> c;
    vector<vector<pii>> e(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        u--;
        v--;
        e[u].emplace_back(v, w);
        e[v].emplace_back(u, w);
    }
    vector dp(n, vector<ll>(MAX, inf));

    auto dfs = [&](auto&& dfs, int u, int f = -1, int fw = 0) -> void {
        ll sum = 0;
        for (auto [v, w] : e[u])
            if (v != f) {
                dfs(dfs, v, u, w);
                sum += *min_element(all(dp[v]));
            }
        for (int i = fw; i < MAX; i++) {
            if (!fw) dp[u][i] = min(dp[u][i], sum + (ll)c * sz(e[u]));
            else dp[u][i] = min(dp[u][i], sum + i - fw + (ll)c * sz(e[u]));
        }
        for (int i = fw; i < MAX; i++) {
            ll tmp = 0;
            bool ok = true;
            for (auto [v, w] : e[u])
                if (v != f) {
                    if (dp[v][i] == inf) {
                        ok = false;
                        break;
                    }
                    tmp += dp[v][i];
                }
            if (ok) {
                if (!fw) dp[u][i] = min(dp[u][i], tmp);
                else dp[u][i] = min(dp[u][i], tmp + i - fw);
            }
        }
    };

    dfs(dfs, 0);
    cout << *min_element(all(dp[0])) << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);
    int T = 1;
    // cin >> T;
    while (T--) solve();
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 5ms
memory: 14120kb

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:

272

result:

ok single line: '272'

Test #2:

score: 0
Accepted
time: 3ms
memory: 10988kb

input:

9 10
1 6 26
2 6 27
3 6 28
4 6 29
5 6 30
7 9 14
8 9 1
9 6 10

output:

60

result:

ok single line: '60'

Test #3:

score: 0
Accepted
time: 6ms
memory: 9368kb

input:

7 64
2 1 194
3 1 187
4 2 158
5 1 42
6 5 101
7 5 80

output:

308

result:

ok single line: '308'

Test #4:

score: 0
Accepted
time: 5ms
memory: 8748kb

input:

6 32
2 1 110
3 2 36
4 1 54
5 3 101
6 5 71

output:

178

result:

ok single line: '178'

Test #5:

score: -100
Memory Limit Exceeded

input:

20000 100
2273 4097 98627
14155 14055 33506
16060 6363 28081
14840 12695 23379
11520 7892 5831
6633 13834 73944
19218 19341 62064
14392 160 58289
18147 209 46443
16941 5453 55103
11895 12849 31201
10275 1622 71781
19595 6349 14232
19485 10800 9778
10745 13541 44786
18727 15264 25726
5847 12815 43894...

output:


result: