Submission #1111864

# Submission time Handle Problem Language Result Execution time Memory
1111864 2024-11-13T08:09:45 Z luvna Museum (CEOI17_museum) C++14
100 / 100
225 ms 140624 KB
#include<bits/stdc++.h>

#define all(v) v.begin(), v.end()
#define endl "\n"

using namespace std;

const int N = 1e4+15;
const int MOD = 1e9 + 7;

int n, k, root;
vector<pair<int,int>> adj[N];
vector<int> f[N], g[N];
int sz[N];

void dfs(int u, int p){
    f[u].resize(sz[u] + 1, MOD);
    g[u].resize(sz[u] + 1, MOD);
    f[u][1] = g[u][1] = 0;
    int siz = 1;
    for(auto [v, w] : adj[u]) if(v != p){
        dfs(v,u);
        for(int i = siz; i >= 1; i--){
            for(int j = sz[v]; j >= 1; j--){
                f[u][i+j] = min({f[u][i+j], f[u][i] + g[v][j] + 2*w, g[u][i] + f[v][j] + w});
                g[u][i+j] = min(g[u][i+j], g[u][i] + g[v][j] + 2*w);
            }
        }
        siz += sz[v];
    }
}

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

void solve(){
    cin >> n >> k >> 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});
    }

    luvna(root,-1);
    dfs(root,-1);

    cout << min(g[root][k], f[root][k]);
}

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

    #define task "task"

    if(fopen(task".INP", "r")){
        freopen(task".INP", "r", stdin);
        freopen(task".OUT", "w", stdout);
    }

    int t; t = 1; //cin >> t;
    while(t--) solve();
}

Compilation message

museum.cpp: In function 'void dfs(int, int)':
museum.cpp:21:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   21 |     for(auto [v, w] : adj[u]) if(v != p){
      |              ^
museum.cpp: In function 'void luvna(int, int)':
museum.cpp:35:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   35 |     for(auto [v, w] : adj[u]) if(v != p){
      |              ^
museum.cpp: In function 'int main()':
museum.cpp:63:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |         freopen(task".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
museum.cpp:64:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |         freopen(task".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1104 KB Output is correct
2 Correct 1 ms 1104 KB Output is correct
3 Correct 1 ms 1104 KB Output is correct
4 Correct 1 ms 1104 KB Output is correct
5 Correct 1 ms 1104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 95 ms 3692 KB Output is correct
2 Correct 95 ms 4168 KB Output is correct
3 Correct 200 ms 136784 KB Output is correct
4 Correct 131 ms 44616 KB Output is correct
5 Correct 102 ms 12628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 95 ms 3692 KB Output is correct
2 Correct 95 ms 4168 KB Output is correct
3 Correct 200 ms 136784 KB Output is correct
4 Correct 131 ms 44616 KB Output is correct
5 Correct 102 ms 12628 KB Output is correct
6 Correct 97 ms 3144 KB Output is correct
7 Correct 169 ms 80712 KB Output is correct
8 Correct 153 ms 2464 KB Output is correct
9 Correct 133 ms 2708 KB Output is correct
10 Correct 99 ms 3144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1104 KB Output is correct
2 Correct 1 ms 1104 KB Output is correct
3 Correct 1 ms 1104 KB Output is correct
4 Correct 1 ms 1104 KB Output is correct
5 Correct 1 ms 1104 KB Output is correct
6 Correct 95 ms 3692 KB Output is correct
7 Correct 95 ms 4168 KB Output is correct
8 Correct 200 ms 136784 KB Output is correct
9 Correct 131 ms 44616 KB Output is correct
10 Correct 102 ms 12628 KB Output is correct
11 Correct 97 ms 3144 KB Output is correct
12 Correct 169 ms 80712 KB Output is correct
13 Correct 153 ms 2464 KB Output is correct
14 Correct 133 ms 2708 KB Output is correct
15 Correct 99 ms 3144 KB Output is correct
16 Correct 96 ms 4528 KB Output is correct
17 Correct 95 ms 4104 KB Output is correct
18 Correct 147 ms 54636 KB Output is correct
19 Correct 135 ms 2384 KB Output is correct
20 Correct 95 ms 3408 KB Output is correct
21 Correct 161 ms 77424 KB Output is correct
22 Correct 94 ms 3912 KB Output is correct
23 Correct 137 ms 2384 KB Output is correct
24 Correct 96 ms 3144 KB Output is correct
25 Correct 225 ms 140624 KB Output is correct