제출 #1125919

#제출 시각아이디문제언어결과실행 시간메모리
1125919tgh317127Museum (CEOI17_museum)C++20
0 / 100
1194 ms785024 KiB
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MASK(i) ((1LL) << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define fi first
#define se second

using namespace std;

const long inf = 1e9 + 7;
const long mod = 1e9 + 7;
const long Nmax = 1e4 + 7;
const long lg = 20;
const long bs = 1003;

int n, k, x;
vector<pii> g[Nmax];
int dp[Nmax][Nmax][2];
int pref[Nmax];
int sz[Nmax];

void combine(int u, int v, int c)
{
    for (int i = 1; i <= sz[u]; i++)
    {
        dp[u][i][1] = min(dp[u][i][1], dp[v][i - 1][1] + c);
        for (int j = 1; j <= sz[v]; j++)
        {
            if (i + j > sz[u]) break;
            dp[u][i + j][0] = min(dp[u][i + j][0], pref[i] + dp[v][j][0] + 2 * c);
            dp[u][i + j][1] = min(dp[u][i + j][1], pref[i] + dp[v][j][1] + c);
        }
    }
    for (int i = 1; i <= sz[u]; i++) pref[i] = min(pref[i], dp[u][i][0]);
}

void dfs(int u, int p)
{
    sz[u] = 1;
    for (auto v : g[u])
    {
        if (v.fi == p) continue;
        dfs(v.fi, u);
        sz[u] += sz[v.fi];
    }
    for (int i = 1; i <= sz[u]; i++) pref[i] = inf;
    pref[0] = pref[1] = 0;
    for (auto v : g[u])
    {
        if (v.fi == p) continue;
        combine(u, v.fi, v.se);
    }
    dp[u][0][0] = dp[u][0][1] = 0;
    dp[u][1][0] = dp[u][1][1] = 0;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("name.inp","r",stdin);
    //freopen("name.out","w",stdout);
    cin >> n >> k >> x;
    for (int i = 1; i < n; i++)
    {
        int u, v, c;
        cin >> u >> v >> c;
        g[u].push_back({v, c});
        g[v].push_back({u, c});
    }
    memset(dp, 0x3f, sizeof dp);
    dfs(x, -1);
    cout << dp[x][k][1] << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...