답안 #800481

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
800481 2023-08-01T15:21:15 Z jakobrs 경주 (Race) (IOI11_race) C++17
100 / 100
284 ms 54092 KB
#include <map>
#include <vector>

template <typename Key, typename Value>
struct Map {
    Key key_offset;
    Value value_offset;
    std::map<Key, Value> m;

    template <typename... Args>
    Map(Args &&...args)
        : m(std::forward<Args>(args)...), key_offset(), value_offset() {}
};

struct Edge {
    int other;
    int length;
};

std::vector<std::vector<Edge>> adj;
int best;
int n, target;

void merge_into(Map<int, int> &into, Map<int, int> &from) {
    if (into.m.size() < from.m.size()) {
        std::swap(into, from);
    }

    for (auto [k_, v_] : from.m) {
        auto k = k_ + from.key_offset;
        auto v = v_ + from.value_offset;

        auto it1 = into.m.find(target - k - into.key_offset);

        if (it1 != into.m.end()) {
            best = std::min(best, v + it1->second + into.value_offset);
        }
    }

    for (auto [k_, v_] : from.m) {
        auto k = k_ + from.key_offset;
        auto v = v_ + from.value_offset;

        auto it1 = into.m.find(k - into.key_offset);
        if (it1 != into.m.end()) {
            it1->second = std::min(it1->second + into.value_offset, v) -
                          into.value_offset;

        } else {
            into.m.insert({k - into.key_offset, v - into.value_offset});
        }
    }
}

Map<int, int> dfs(int node, int parent) {
    Map<int, int> result;
    result.m.insert({0, 0});

    for (Edge e : adj[node]) {
        if (e.other == parent) continue;

        Map m = dfs(e.other, node);
        m.key_offset += e.length;
        m.value_offset += 1;

        merge_into(result, m);
    }

    return result;
}

int best_path(int N, int K, int H[][2], int L[]) {
    adj.resize(N);
    best = N;
    n = N;
    target = K;
    for (int i = 0; i < N - 1; i++) {
        adj[H[i][0]].push_back({H[i][1], L[i]});
        adj[H[i][1]].push_back({H[i][0], L[i]});
    }

    dfs(0, -1);

    if (best == N)
        return -1;
    else
        return best;
}

Compilation message

race.cpp: In instantiation of 'Map<Key, Value>::Map(Args&& ...) [with Args = {}; Key = int; Value = int]':
race.cpp:56:19:   required from here
race.cpp:8:26: warning: 'Map<int, int>::m' will be initialized after [-Wreorder]
    8 |     std::map<Key, Value> m;
      |                          ^
race.cpp:6:9: warning:   'int Map<int, int>::key_offset' [-Wreorder]
    6 |     Key key_offset;
      |         ^~~~~~~~~~
race.cpp:11:5: warning:   when initialized here [-Wreorder]
   11 |     Map(Args &&...args)
      |     ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 292 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 296 KB Output is correct
17 Correct 1 ms 292 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 292 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 296 KB Output is correct
17 Correct 1 ms 292 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 352 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 436 KB Output is correct
29 Correct 1 ms 440 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 436 KB Output is correct
33 Correct 1 ms 436 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 432 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 292 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 296 KB Output is correct
17 Correct 1 ms 292 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 66 ms 8864 KB Output is correct
20 Correct 53 ms 8780 KB Output is correct
21 Correct 54 ms 8892 KB Output is correct
22 Correct 53 ms 8908 KB Output is correct
23 Correct 75 ms 9332 KB Output is correct
24 Correct 57 ms 9076 KB Output is correct
25 Correct 46 ms 18324 KB Output is correct
26 Correct 43 ms 26956 KB Output is correct
27 Correct 134 ms 17976 KB Output is correct
28 Correct 170 ms 54092 KB Output is correct
29 Correct 163 ms 52736 KB Output is correct
30 Correct 97 ms 17972 KB Output is correct
31 Correct 96 ms 17920 KB Output is correct
32 Correct 103 ms 18076 KB Output is correct
33 Correct 100 ms 17208 KB Output is correct
34 Correct 199 ms 27580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 292 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 296 KB Output is correct
17 Correct 1 ms 292 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 352 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 436 KB Output is correct
29 Correct 1 ms 440 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 436 KB Output is correct
33 Correct 1 ms 436 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 432 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 66 ms 8864 KB Output is correct
40 Correct 53 ms 8780 KB Output is correct
41 Correct 54 ms 8892 KB Output is correct
42 Correct 53 ms 8908 KB Output is correct
43 Correct 75 ms 9332 KB Output is correct
44 Correct 57 ms 9076 KB Output is correct
45 Correct 46 ms 18324 KB Output is correct
46 Correct 43 ms 26956 KB Output is correct
47 Correct 134 ms 17976 KB Output is correct
48 Correct 170 ms 54092 KB Output is correct
49 Correct 163 ms 52736 KB Output is correct
50 Correct 97 ms 17972 KB Output is correct
51 Correct 96 ms 17920 KB Output is correct
52 Correct 103 ms 18076 KB Output is correct
53 Correct 100 ms 17208 KB Output is correct
54 Correct 199 ms 27580 KB Output is correct
55 Correct 9 ms 1492 KB Output is correct
56 Correct 5 ms 1108 KB Output is correct
57 Correct 38 ms 9028 KB Output is correct
58 Correct 30 ms 8740 KB Output is correct
59 Correct 65 ms 27008 KB Output is correct
60 Correct 165 ms 52972 KB Output is correct
61 Correct 117 ms 19380 KB Output is correct
62 Correct 93 ms 18052 KB Output is correct
63 Correct 99 ms 18116 KB Output is correct
64 Correct 284 ms 24512 KB Output is correct
65 Correct 274 ms 28076 KB Output is correct
66 Correct 185 ms 49776 KB Output is correct
67 Correct 72 ms 18484 KB Output is correct
68 Correct 208 ms 26516 KB Output is correct
69 Correct 235 ms 30028 KB Output is correct
70 Correct 193 ms 25596 KB Output is correct