제출 #1323931

#제출 시각아이디문제언어결과실행 시간메모리
1323931AestivateMuseum (CEOI17_museum)C++20
80 / 100
2192 ms1114112 KiB
#include <bits/stdc++.h>
using namespace std;

// #define int long long
#define F first 
#define S second 
#define pb push_back

vector<pair<int, int>>node[10001];
int f[10001][10001][2];
int sz[10001];
int n, k, x;

void dfs(int v, int pre){
    vector<vector<int>>calc(k+1, vector<int>(2, 1e9));

    calc[1][0]=0, calc[1][1]=0;
    sz[v]=1;
    for(auto kk: node[v]){
        int gg=kk.F;int c=kk.S;
        if(gg==pre) continue;
        dfs(gg, v);
        for(int i=sz[v];i>=1;i--){
            for(int j=1;j<=sz[gg];j++){
                if(i+j>k) break;
                calc[i+j][0]=min(calc[i+j][0], calc[i][0]+f[gg][j][0]+c*2);
                calc[i+j][1]=min({calc[i+j][1], calc[i][1]+c*2+f[gg][j][0], calc[i][0]+c+f[gg][j][1]});
            }
        }
        sz[v]+=sz[gg];
    }
    for(int i=0;i<=sz[v];i++){
        if(i>k) break;
        f[v][i][0]=calc[i][0];
        f[v][i][1]=calc[i][1];
    }
}

void solve(){
    cin>>n>>k>>x;
    
    for(int i=1;i<n;i++){
        int g, h, c;
        cin>>g>>h>>c;
        node[g].pb({h, c});
        node[h].pb({g, c});
    }
    dfs(x, x);
    cout<<f[x][k][1]<<"\n";
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    // int t;
    // cin>>t;
    // while(t--)
    solve();
    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...