답안 #468332

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
468332 2021-08-27T16:15:57 Z Vladth11 Museum (CEOI17_museum) C++14
100 / 100
329 ms 180804 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
#pragma GCC optimize("Ofast,unroll-loops")

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <long double, pii> muchie;
typedef tree <ll, null_type, less_equal <ll>, rb_tree_tag, tree_order_statistics_node_update> OST;

const ll NMAX = 10001;
const ll INF = (1LL << 60);
const ll HALF = (1LL << 59);
const ll MOD = 30013;
const ll BLOCK = 318;
const ll base = 31;
const ll nr_of_bits = 21;

int dp[NMAX][NMAX][2];
int n, k, root;
vector <pii> v[NMAX];
int sz[NMAX];

void DFS(int node, int p) {
    int i;
    dp[node][1][0] = dp[node][1][1] = 0;
    sz[node] = 1;
    for(auto x : v[node]) {
        if(x.first == p)
            continue;
        DFS(x.first, node);
        sz[node] += sz[x.first];
    }
    for(i = 2; i <= min(k, sz[node]); i++) {
        dp[node][i][0] = dp[node][i][1] = 1e9;
    }
    int sum = 1;

    for(auto x : v[node]) {
        if(x.first == p)
            continue;
        for(int total = min(k, sum); total >= 1; total--) {
            //debugs(total);
            for(int subtree = 1; subtree <= min(k - total, sz[x.first]); subtree++) {
                dp[node][total + subtree][0] = min(dp[node][total][0] + dp[x.first][subtree][0] + x.second * 2, dp[node][total + subtree][0]);
                dp[node][total + subtree][1] = min(dp[node][total][1] + dp[x.first][subtree][0] + x.second * 2, dp[node][total + subtree][1]);
                dp[node][total + subtree][1] = min(dp[node][total][0] + dp[x.first][subtree][1] + x.second, dp[node][total + subtree][1]);
            }
        }

        sum += sz[x.first];
    }
}

int main() {
    int i;
    cin >> n >> k >> root;
    for(i = 1; i < n; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        v[a].push_back({b, c});
        v[b].push_back({a, c});
    }
    DFS(root, 0);
    cout << dp[root][k][1];
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 588 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 532 KB Output is correct
5 Correct 1 ms 540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 43104 KB Output is correct
2 Correct 36 ms 43204 KB Output is correct
3 Correct 36 ms 45828 KB Output is correct
4 Correct 34 ms 44416 KB Output is correct
5 Correct 33 ms 44080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 43104 KB Output is correct
2 Correct 36 ms 43204 KB Output is correct
3 Correct 36 ms 45828 KB Output is correct
4 Correct 34 ms 44416 KB Output is correct
5 Correct 33 ms 44080 KB Output is correct
6 Correct 32 ms 42956 KB Output is correct
7 Correct 34 ms 45684 KB Output is correct
8 Correct 40 ms 42540 KB Output is correct
9 Correct 43 ms 42596 KB Output is correct
10 Correct 34 ms 42740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 588 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 532 KB Output is correct
5 Correct 1 ms 540 KB Output is correct
6 Correct 32 ms 43104 KB Output is correct
7 Correct 36 ms 43204 KB Output is correct
8 Correct 36 ms 45828 KB Output is correct
9 Correct 34 ms 44416 KB Output is correct
10 Correct 33 ms 44080 KB Output is correct
11 Correct 32 ms 42956 KB Output is correct
12 Correct 34 ms 45684 KB Output is correct
13 Correct 40 ms 42540 KB Output is correct
14 Correct 43 ms 42596 KB Output is correct
15 Correct 34 ms 42740 KB Output is correct
16 Correct 43 ms 43544 KB Output is correct
17 Correct 84 ms 44116 KB Output is correct
18 Correct 55 ms 56984 KB Output is correct
19 Correct 98 ms 42752 KB Output is correct
20 Correct 47 ms 42948 KB Output is correct
21 Correct 153 ms 118024 KB Output is correct
22 Correct 103 ms 44336 KB Output is correct
23 Correct 329 ms 42712 KB Output is correct
24 Correct 109 ms 43456 KB Output is correct
25 Correct 195 ms 180804 KB Output is correct