Submission #922930

#TimeUsernameProblemLanguageResultExecution timeMemory
922930Andrei_ierdnAMuseum (CEOI17_museum)C++17
100 / 100
205 ms233724 KiB
#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

#define MAXN 10000
#define INF 1000000000

struct Edge
{
    int a, b, c;
    int other(int node) {
        return (node == a) ? b : a;
    }
};

int n, k, x, i, sz[MAXN+2];
int dpcyc[MAXN+2][MAXN+2], dp[MAXN+2][MAXN+2];
vector<int> nb[MAXN+2];
Edge edges[MAXN+2];

void dfs1(int node)
{
    sz[node] = 1;
    dpcyc[node][1] = dp[node][1] = 0;
    for (auto e : nb[node]) {
        int x = edges[e].other(node);
        int c = edges[e].c;
        if (!sz[x]) {
            dfs1(x);
            for (int i = sz[node]+1; i <= sz[node]+sz[x]; i++) {
                dpcyc[node][i] = dp[node][i] = INF;
            }
            for (int i = sz[node]; i >= 1; i--) {
                for (int j = sz[x]; j >= 1; j--) {
                    dpcyc[node][i+j] = min(dpcyc[node][i+j], dpcyc[node][i] + dpcyc[x][j] + 2*c);
                    dp[node][i+j] = min(dp[node][i+j], dp[node][i] + dpcyc[x][j] + 2*c);
                    dp[node][i+j] = min(dp[node][i+j], dpcyc[node][i] + dp[x][j] + c);
                }
            }
            sz[node] += sz[x];
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> k >> x;
    for (i = 1; i < n; i++) {
        cin >> edges[i].a >> edges[i].b >> edges[i].c;
        nb[edges[i].a].push_back(i);
        nb[edges[i].b].push_back(i);
    }
    dfs1(x);
    cout << dp[x][k];
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...