Submission #884456

#TimeUsernameProblemLanguageResultExecution timeMemory
884456Peter2017Museum (CEOI17_museum)C++14
100 / 100
321 ms785316 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pill pair<int,ll>
#define mem(a, b) memset(a, b, sizeof(a))
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define name "test"

using namespace std;

const int N = 1e4 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;

template <typename T1, typename T2> bool maxi(T1 &a, T2 b){if (a < b){a = b; return 1;} return 0;}
template <typename T1, typename T2> bool mini(T1 &a, T2 b){if (a > b){a = b; return 1;} return 0;}

int n;
int rooms;
int root;
int sz[N];
int g[N][2];
int f[N][N][2];
vector <pii> adj[N];

void load(){
    cin.tie(0)->sync_with_stdio(0);
    if (fopen(name".inp", "r")){
        freopen(name".inp", "r", stdin);
        freopen(name".out", "w", stdout);
    }
}

void input(){
    cin >> n >> rooms >> root;
    for (int i = 1; i < n; i++){
        int u, v, w;
        cin >> u >> v >> w;
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }
}

void combine(int u, int v, int w){
    for (int i = 0; i <= min(rooms, sz[u] + sz[v]); i++)
        for (int j = 0; j <= 1; j++)
            g[i][j] = mod;
    for (int i = 1; i <= min(rooms, sz[u]); i++)
        for (int j = 0; j <= min(rooms - i, sz[v]); j++){
                mini(g[i + j][0], f[u][i][1] + f[v][j][0] + w);
                mini(g[i + j][0], f[u][i][0] + f[v][j][1] + 2 * w);
                mini(g[i + j][1], f[u][i][1] + f[v][j][1] + 2 * w);
        }
    for (int i = 1; i <= min(rooms, sz[u] + sz[v]); i++)
        for (int j = 0; j <= 1; j++)
            mini(f[u][i][j], g[i][j]);
}

void dfs(int u, int pre){
    sz[u] = 1;
    f[u][1][0] = 0;
    f[u][1][1] = 0;
    for (auto it : adj[u]){
        int v = it.fi;
        int w = it.se;
        if (v != pre){
            dfs(v, u);
            combine(u, v, w);
            sz[u] += sz[v];
        }
    }
}

void solve(){
    mem(f, 0x3f);
    dfs(root, root);
    cout << f[root][rooms][0];
}

int main(){
    load();
    input();
    solve();
}

Compilation message (stderr)

museum.cpp: In function 'void load()':
museum.cpp:33:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |         freopen(name".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
museum.cpp:34:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |         freopen(name".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...