Submission #1291459

#TimeUsernameProblemLanguageResultExecution timeMemory
1291459phucleMuseum (CEOI17_museum)C++20
20 / 100
530 ms1114112 KiB
#include <bits/stdc++.h>
#define FOD(i, a, b) for (int i = a; i <= b; ++i)
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define ii pair<int, int>

using namespace std;

const int N = 10005;
const int mod = 1e9 + 7;
const long long inf = 1e16;

int n, X, K;
int sz[N];
long long dp[N][N][2];
vector <ii> g[N];

void dfs(int u, int p = -1) {
    sz[u]++;
    for (auto [v, w] : g[u]) if (v != p) {
        dfs(v, u);
    }

    dp[u][1][0] = dp[u][1][1] = 0;
    for (int i = 2; i <= n; ++i) dp[u][i][0] = dp[u][i][1] = inf;

    for (auto [v, w] : g[u]) if(v != p) {

        for (int i = sz[u]; i >= 1; --i) {
            if (dp[u][i][0] >= inf || dp[u][i][1] >= inf) continue;
            for (int j = 1; j <= sz[v]; ++j) {
                long long cost0 = dp[v][j][0] + w;
                long long cost1 = dp[v][j][1] + 2 * w;
                dp[u][i + j][0] = min(dp[u][i + j][0], dp[u][i][1] + cost0);
                dp[u][i + j][0] = min(dp[u][i + j][0], dp[u][i][0] + cost1);
                dp[u][i + j][1] = min(dp[u][i + j][1], dp[u][i][1] + cost1);
            }
        }

        sz[u] += sz[v];
    }
}

main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n >> K >> X;
    for (int i = 1; i < n; ++i) {
        int u, v, w; cin >> u >> v >> w;
        g[u].emplace_back(v, w);
        g[v].emplace_back(u, w);
    }

    dfs(X);

    cout << dp[X][K][0] << '\n';

    return 0;
}

Compilation message (stderr)

museum.cpp:45:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   45 | main()
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...