답안 #788809

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
788809 2023-07-20T16:06:17 Z LeVanThuc Museum (CEOI17_museum) C++17
100 / 100
192 ms 140628 KB
#include<bits/stdc++.h>
using namespace std;
#define ll int
#define fi first
#define se second
#define p(x,y) pair<ll,ll>(x,y)
#define BIT(i,x) ((x>>i)&1)
#define MASK(x) (1<<x)
#define ld long double
#define __builtin_popcount __builtin_popcountll
#define pll pair<ll,ll>
template<class T1,class T2>
bool maximize(T1 &x,const T2 &y)
{
    if(x<y)
    {
        x=y;
        return 1;
    }
    return 0;
}
template<class T1,class T2>
bool minimize(T1 &x,const T2 &y)
{
    if(x>y)
    {
        x=y;
        return 1;
    }
    return 0;
}
void online()
{
    std::ios_base::sync_with_stdio(0);
    cin.tie(0);
#ifndef ONLINE_JUDGE
//    freopen("input.inp", "r", stdin);
//    freopen("output.out","w", stdout);
#else
#endif
}
const ll N=1e4+10,M=5e8+7;
vector<ll> f[N][2];
ll f1[2][N],tt,n,x,c[N],par[N],cost[N];
vector<pll> gr[N];
void dfs(ll u)
{
    ll h=0;
    c[u]=1;
    for(auto [v,w]:gr[u])
    {
        if(v==par[u]) continue;
        par[v]=u;
        cost[v]=w;
        dfs(v);
        c[u]+=c[v];
        if(c[v]>c[h]) h=v;
    }
    f[u][0].resize(min(c[u],tt)+2,M);
    f[u][1].resize(min(c[u],tt)+2,M);
    f[u][0][1]=f[u][1][1]=0;
    ll cr=1;
    if(h)
    {
        for(int i=1;i<=min(c[h],tt);i++)
        {
            minimize(f[u][0][i+1],f[h][0][i]+2*cost[h]);
            minimize(f[u][1][i+1],f[h][1][i]+cost[h]);
        }
        cr+=c[h];
    }
    for(auto [v,w]:gr[u])
    {
        if(v==h||v==par[u]) continue;
        cr+=c[v];
        minimize(cr,tt);
        for(int k=0;k<=cr;k++) f1[0][k]=f[u][0][k],f1[1][k]=f[u][1][k];
        for(int k=2;k<=cr;k++)
        {
            for(int j=1;j<=min(k-1,c[v]);j++)
            {
                minimize(f1[0][k],f[u][0][k-j]+f[v][0][j]+2*w);
                minimize(f1[1][k],f[u][1][k-j]+f[v][0][j]+2*w);
                minimize(f1[1][k],f[u][0][k-j]+f[v][1][j]+w);
            }
        }
        for(int k=0;k<=cr;k++) f[u][0][k]=f1[0][k],f[u][1][k]=f1[1][k];
    }
}
int main()
{
    online();
    cin>>n>>tt>>x;
    for(int i=1;i<n;i++)
    {
        ll u,v,w;
        cin>>u>>v>>w;
        gr[u].emplace_back(v,w);
        gr[v].emplace_back(u,w);
    }
    dfs(x);
    cout<<f[x][1][tt];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 980 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 980 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 2772 KB Output is correct
2 Correct 9 ms 2760 KB Output is correct
3 Correct 10 ms 5340 KB Output is correct
4 Correct 9 ms 4052 KB Output is correct
5 Correct 9 ms 3540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 2772 KB Output is correct
2 Correct 9 ms 2760 KB Output is correct
3 Correct 10 ms 5340 KB Output is correct
4 Correct 9 ms 4052 KB Output is correct
5 Correct 9 ms 3540 KB Output is correct
6 Correct 9 ms 2516 KB Output is correct
7 Correct 10 ms 5076 KB Output is correct
8 Correct 8 ms 2324 KB Output is correct
9 Correct 10 ms 2260 KB Output is correct
10 Correct 8 ms 2388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 980 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 980 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
6 Correct 9 ms 2772 KB Output is correct
7 Correct 9 ms 2760 KB Output is correct
8 Correct 10 ms 5340 KB Output is correct
9 Correct 9 ms 4052 KB Output is correct
10 Correct 9 ms 3540 KB Output is correct
11 Correct 9 ms 2516 KB Output is correct
12 Correct 10 ms 5076 KB Output is correct
13 Correct 8 ms 2324 KB Output is correct
14 Correct 10 ms 2260 KB Output is correct
15 Correct 8 ms 2388 KB Output is correct
16 Correct 24 ms 3120 KB Output is correct
17 Correct 83 ms 3676 KB Output is correct
18 Correct 32 ms 16480 KB Output is correct
19 Correct 38 ms 2252 KB Output is correct
20 Correct 23 ms 2712 KB Output is correct
21 Correct 134 ms 77568 KB Output is correct
22 Correct 95 ms 4040 KB Output is correct
23 Correct 192 ms 2472 KB Output is correct
24 Correct 99 ms 3252 KB Output is correct
25 Correct 168 ms 140628 KB Output is correct