QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#183063#2161. The Cost of Speed Limitsquocanh19082010AC ✓431ms788844kbC++142.1kb2023-09-18 21:56:022023-09-18 21:56:02

Judging History

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

  • [2023-09-18 21:56:02]
  • 评测
  • 测评结果:AC
  • 用时:431ms
  • 内存:788844kb
  • [2023-09-18 21:56:02]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    unsigned int c;
    cin >> n >> c;
    vector <vector <pair <int, unsigned int>>> adj(n);
    vector <unsigned int> all_S;
    for (int i = 0; i < n - 1; ++i) {
        int a, b;
        unsigned int s;
        cin >> a >> b >> s;
        --a;
        --b;
        adj[a].emplace_back(b, s);
        adj[b].emplace_back(a, s);
        all_S.push_back(s);
    }
    sort(all_S.begin(), all_S.end());
    all_S.erase(unique(all_S.begin(), all_S.end()), all_S.end());
    reverse(all_S.begin(), all_S.end());
    constexpr unsigned int inf = numeric_limits <unsigned int>::max();
    vector <vector <unsigned int>> v_take(n);
    auto Dfs = [&](auto self, int root, int skip) {
        unsigned int best_n = c * adj[root].size();
        unsigned int max_adj = 0;
        unsigned int upwards = 0;
        for (auto e : adj[root]) {
            max_adj = max(max_adj, e.second);
            if (e.first == skip) upwards = e.second;
        }
        int count = all_S.size();
        while (count > 0 && all_S[count - 1] < max_adj) --count;
        v_take[root].resize(count);
        auto& best_t = v_take[root];
        for (int i = 0; i < count; ++i) {
            if (upwards > 0) best_t[i] = all_S[i] - upwards;
        }
        if (root == -1) return best_n;
        for (auto e : adj[root]) {
            if (e.first == skip) continue;
            auto child_ntake = self(self, e.first, root);
            const auto& child_take = v_take[e.first];
            unsigned int best_child = child_ntake;
            for (auto x : child_take) best_child = min(best_child, x);
            best_n += best_child;
            for (int i = 0; i < count; ++i) {
                unsigned int cur = child_ntake + all_S[i] - e.second;
                if (i < child_take.size()) cur = min(cur, child_take[i]);
                best_t[i] += cur;
            }
        }
        return best_n;
    };
    unsigned int ans = Dfs(Dfs, 0, -1);
    for (unsigned int x : v_take[0]) ans = min(ans, x);
    cout << ans << "\n";
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3488kb

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: 1ms
memory: 3628kb

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: 0ms
memory: 3772kb

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: 0ms
memory: 3828kb

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: 0
Accepted
time: 256ms
memory: 538472kb

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:

2999800

result:

ok single line: '2999800'

Test #6:

score: 0
Accepted
time: 431ms
memory: 788816kb

input:

20000 1000
1 2 99998
2 3 99994
3 4 99992
4 5 99989
5 6 99987
6 7 99983
7 8 99982
8 9 99978
9 10 99973
10 11 99970
11 12 99965
12 13 99961
13 14 99957
14 15 99954
15 16 99949
16 17 99946
17 18 99945
18 19 99940
19 20 99935
20 21 99932
21 22 99927
22 23 99923
23 24 99920
24 25 99919
25 26 99918
26 27 ...

output:

2088429

result:

ok single line: '2088429'

Test #7:

score: 0
Accepted
time: 414ms
memory: 788844kb

input:

20000 1000
1 2 4
2 3 6
3 4 9
4 5 13
5 6 15
6 7 17
7 8 22
8 9 23
9 10 27
10 11 28
11 12 31
12 13 36
13 14 38
14 15 39
15 16 44
16 17 46
17 18 49
18 19 52
19 20 55
20 21 60
21 22 62
22 23 64
23 24 67
24 25 68
25 26 71
26 27 72
27 28 76
28 29 77
29 30 82
30 31 86
31 32 89
32 33 90
33 34 95
34 35 96
35 ...

output:

2098509

result:

ok single line: '2098509'

Test #8:

score: 0
Accepted
time: 10ms
memory: 5596kb

input:

20000 100000
1 4 1
1 5 1
1 6 1
1 7 1
1 8 1
1 9 1
1 10 1
1 11 1
1 12 1
1 13 1
1 14 1
1 15 1
1 16 1
1 17 1
1 18 1
1 19 1
1 20 1
1 21 1
1 22 1
1 23 1
1 24 1
1 25 1
1 26 1
1 27 1
1 28 1
1 29 1
1 30 1
1 31 1
1 32 1
1 33 1
1 34 1
1 35 1
1 36 1
1 37 1
1 38 1
1 39 1
1 40 1
1 41 1
1 42 1
1 43 1
1 44 1
1 45 1...

output:

1999780001

result:

ok single line: '1999780001'

Test #9:

score: 0
Accepted
time: 14ms
memory: 12848kb

input:

20000 100
1 2 114
2 3 243
3 4 243
4 5 239
5 6 248
6 7 122
7 8 187
8 9 281
9 10 241
10 11 209
11 12 119
12 13 241
13 14 243
14 15 236
15 16 151
16 17 272
17 18 132
18 19 281
19 20 272
20 21 194
21 22 233
22 23 220
23 24 271
24 25 278
25 26 153
26 27 108
27 28 229
28 29 130
29 30 291
30 31 245
31 32 2...

output:

1636347

result:

ok single line: '1636347'

Test #10:

score: 0
Accepted
time: 0ms
memory: 3552kb

input:

1 12345

output:

0

result:

ok single line: '0'

Test #11:

score: 0
Accepted
time: 0ms
memory: 3652kb

input:

9 50
1 2 1000
2 3 995
3 4 990
4 5 998
5 6 993
6 7 991
7 8 996
8 9 999

output:

38

result:

ok single line: '38'

Test #12:

score: 0
Accepted
time: 0ms
memory: 3856kb

input:

9 1
1 2 1000
2 3 995
3 4 990
4 5 998
5 6 993
6 7 991
7 8 996
8 9 999

output:

14

result:

ok single line: '14'

Test #13:

score: 0
Accepted
time: 0ms
memory: 3596kb

input:

14 50
2 12 930
2 13 951
2 14 920
2 3 999
3 4 995
4 5 990
6 5 1000
6 7 995
7 8 990
8 9 998
9 10 900
9 11 901
9 1 1001

output:

432

result:

ok single line: '432'

Test #14:

score: 0
Accepted
time: 0ms
memory: 3540kb

input:

14 50
2 12 930
2 13 951
2 14 920
2 3 999
3 4 995
4 5 990
6 5 1000
6 7 995
7 8 990
8 9 998
9 10 911
9 11 911
9 1 1001

output:

420

result:

ok single line: '420'

Test #15:

score: 0
Accepted
time: 0ms
memory: 3544kb

input:

4 16
2 1 2
3 1 20
4 2 3

output:

33

result:

ok single line: '33'

Test #16:

score: 0
Accepted
time: 0ms
memory: 3852kb

input:

4 1
1 2 4
2 3 1
3 4 4

output:

3

result:

ok single line: '3'

Test #17:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

3 1
1 2 4242
2 3 4242

output:

0

result:

ok single line: '0'

Test #18:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

3 13
2 3 1234
3 1 1261

output:

26

result:

ok single line: '26'

Test #19:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

3 13
2 3 1234
3 1 1259

output:

25

result:

ok single line: '25'

Test #20:

score: 0
Accepted
time: 0ms
memory: 3536kb

input:

2 10
1 2 3

output:

0

result:

ok single line: '0'

Test #21:

score: 0
Accepted
time: 0ms
memory: 3600kb

input:

6 10
1 2 42
1 3 42
1 4 42
1 5 42
1 6 42

output:

0

result:

ok single line: '0'

Test #22:

score: 0
Accepted
time: 0ms
memory: 3772kb

input:

6 8
1 2 42
1 3 42
1 4 41
1 5 42
1 6 52

output:

40

result:

ok single line: '40'

Test #23:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

6 8
1 2 42
1 3 42
1 4 43
1 5 42
1 6 52

output:

39

result:

ok single line: '39'

Test #24:

score: 0
Accepted
time: 18ms
memory: 8140kb

input:

20000 114
17256 6284 130
7416 2964 121
2817 1233 60
19820 1850 55
4446 5382 66
634 17207 65
2229 8149 55
10126 13119 92
16897 17923 120
8466 11376 89
11398 9509 76
19710 2954 82
11765 4918 129
16179 4846 72
10760 928 132
9676 2592 127
279 924 87
8927 147 70
13174 8697 119
2473 2591 143
14540 250 112...

output:

976682

result:

ok single line: '976682'

Test #25:

score: 0
Accepted
time: 0ms
memory: 3836kb

input:

30 100
8 21 62
27 12 136
4 7 129
30 19 148
6 15 167
5 15 163
12 28 96
11 22 108
19 15 99
29 10 124
29 4 78
26 7 86
14 17 55
12 19 157
25 16 68
9 19 139
15 23 139
12 18 62
17 4 81
19 3 75
1 7 81
3 20 87
19 21 108
7 19 53
8 25 54
24 8 176
27 13 72
11 9 72
2 1 65

output:

1941

result:

ok single line: '1941'

Test #26:

score: 0
Accepted
time: 1ms
memory: 3872kb

input:

1000 100
46 565 86
185 862 78
114 897 167
725 211 184
725 352 59
921 693 171
825 435 167
810 295 85
485 454 97
120 666 150
218 654 187
477 233 158
63 188 116
125 157 168
306 345 153
358 778 103
113 392 150
478 684 77
34 223 195
126 627 105
75 60 134
245 345 123
513 127 114
892 877 128
255 889 173
82...

output:

68484

result:

ok single line: '68484'

Test #27:

score: 0
Accepted
time: 1ms
memory: 4108kb

input:

1000 100
566 379 105
576 863 184
141 977 111
269 233 101
115 967 134
579 643 119
32 578 174
505 734 88
265 160 62
753 103 197
613 867 80
765 724 143
854 572 196
543 343 99
267 87 167
998 291 75
939 655 113
951 104 130
396 509 129
553 844 56
781 332 190
140 935 76
523 83 53
292 397 65
755 644 66
625 ...

output:

70300

result:

ok single line: '70300'

Test #28:

score: 0
Accepted
time: 141ms
memory: 271340kb

input:

20000 5000
17376 11705 7422
2530 17050 9107
9626 19120 10732
17610 11666 11477
1550 4663 6845
1803 4833 10251
4026 12638 3425
16802 4034 10734
14798 13444 5733
10846 4641 6718
17227 2308 10391
14179 17972 8563
13674 17434 5375
5220 8359 2123
2964 17402 9752
16560 18032 11242
8745 8346 10280
17268 66...

output:

86862385

result:

ok single line: '86862385'

Test #29:

score: 0
Accepted
time: 14ms
memory: 5984kb

input:

20000 10
11791 469 5
17528 11192 9
19947 13698 16
117 16706 2
13618 5409 18
14605 2812 2
18998 18337 12
12717 13416 15
13138 7301 13
4812 19992 14
13928 8266 13
12255 11261 20
4283 9389 13
2484 5297 12
8479 13325 17
6970 8791 1
19983 6412 12
16464 7951 4
9802 13019 6
5435 9348 13
3328 9520 15
12469 ...

output:

169195

result:

ok single line: '169195'

Test #30:

score: 0
Accepted
time: 47ms
memory: 70532kb

input:

20000 1000
6035 4116 2232
10543 12865 1803
2751 3284 2316
12396 8364 2245
5047 13125 1906
9593 19366 733
16293 2477 751
15415 6663 905
16593 10792 2283
6570 2279 1201
12963 4900 1874
11612 16671 1802
4405 9116 871
17590 15322 637
1675 12432 2061
12081 10399 1472
15178 18967 1166
4396 14197 713
7817 ...

output:

18227896

result:

ok single line: '18227896'