답안 #218598

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
218598 2020-04-02T11:14:34 Z toxic_hack Museum (CEOI17_museum) C++14
80 / 100
3000 ms 804032 KB
// g++ -std=c++14

/*

Today might be the chance to grasp the chance to let your talent bloom.
May be tomorrow, the day after, or next year...
May be even when you are 30. I'm not sure if physique has anything to do with it
but if you think that it will never come, it probably never will.

- Tooru Oikawa.

*/


#include<bits/stdc++.h>

typedef long long ll;
typedef long double lld;
using namespace std;

#define endl "\n"
#define fi first
#define se second
#define MEMS(a,b) memset(a,b,sizeof(a))
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define all(c) c.begin(),c.end()
#define pii pair<int, int>

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r)
{
	uniform_int_distribution<int> uid(l, r);
	return uid(rng);
}

#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)
template<typename S, typename T>
ostream& operator<<(ostream& out,pair<S,T> const& p){out<<'('<<p.fi<<", "<<p.se<<')';return out;}

template<typename T>
ostream& operator<<(ostream& out,vector<T> const& v){
ll l=v.size();for(ll i=0;i<l-1;i++)out<<v[i]<<' ';if(l>0)out<<v[l-1];return out;}

template <typename T>
ostream &operator<<(ostream &out, set<T> const &v) {
    for (auto i = v.begin(); i != v.end(); i++)
        out << (*i) << ' ';
    return out;
}
template <typename T>
ostream &operator<<(ostream &out, multiset<T> const &v) {
    for (auto i = v.begin(); i != v.end(); i++)
        out << (*i) << ' ';
    return out;
}
template <typename T, typename V>
ostream &operator<<(ostream &out, map<T, V> const &v) {
    for (auto i = v.begin(); i != v.end(); i++)
        out << "\n" << (i->first) <<  ":"  << (i->second);
    return out;
}

template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}

template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}

const int N = 1e4 + 2;
vector<vector<pii>> g(N);
int dp[N][N][2]; // 0-> for coming back, 1 -> staying
int n, k, x;
const int inf = 1e9 + 1;
int sub[N];

void dfs(int v, int p) {
    sub[v] = 1;
    vector<vector<int>> tdp(k + 1, vector<int>(2, inf));
    tdp[1][0] = 0;
    tdp[1][1] = 0;
    for (auto u : g[v]) {
        if (u.fi == p) continue;
        dfs(u.fi, v);
        sub[v] += sub[u.fi];
    }

    for (auto u : g[v]) {
        if (u.fi == p) continue;
        // tr(u, v);
        // for (int q = 1; q <= k; q++) {
        //     tr(q, tdp[q]);
        // }
        for (int q = 1; q >= 0; q--) {
            if (q == 0) {
                for (int j = k; j > 0; j--) {
                    for (int i = 1; i <= sub[u.fi] && j - i >= 0; i++) {
                        if (dp[u.fi][i][0] != inf) {
                            tdp[j][0] = min(tdp[j][0], dp[u.fi][i][0] + tdp[j - i][0] + (u.se * 2));
                        } 
                    }
                }
            } else if (q == 1) {
                for (int j = k; j > 0; j--) {
                    for (int i = 0; i <= sub[u.fi] && j - i >= 0; i++) {
                        if (dp[u.fi][i][0] != inf) {
                            tdp[j][1] = min(tdp[j][1], dp[u.fi][i][1] + tdp[j - i][0] + u.se);
                            tdp[j][1] = min(tdp[j][1], dp[u.fi][i][0] + tdp[j - i][1] + (u.se) * 2);
                        } 
                    }
                }
            }
        }
    }

    for (int i = 0; i <= k; i++) {
        dp[v][i][0] = tdp[i][0];
        dp[v][i][1] = tdp[i][1];
    }
    dp[v][0][0] = dp[v][0][1] = 0;
    // tr(v);
    // for (int i = 1; i <= k; i++) {
    //     cout << dp[v][i][0] << " ";
    // }
    // cout << endl;
    // for (int i = 1; i <= k; i++) {
    //     cout << dp[v][i][1] << " ";
    // }
    // cout << endl;
}

int solve() {
    for (int i = 0; i < N; i++) 
        for (int j = 0; j < N; j++) 
            for (int k = 0; k < 2; k++) 
                dp[i][j][k] = inf;

    cin >> n >> k >> x;
    for (int i = 0; i < n - 1; i++) {
        int v, u, w;
        cin >> v >> u >> w;
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }
    dfs(x, 0);
    cout << dp[x][k][1] << endl;
    return 0;
}

int32_t main(){ _
    int t;
    // cin >> t;
    t = 1;
    while (t--) solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 403 ms 783736 KB Output is correct
2 Correct 401 ms 783608 KB Output is correct
3 Correct 418 ms 783864 KB Output is correct
4 Correct 434 ms 783736 KB Output is correct
5 Correct 409 ms 783608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 518 ms 784504 KB Output is correct
2 Correct 540 ms 784504 KB Output is correct
3 Correct 638 ms 804032 KB Output is correct
4 Correct 598 ms 791476 KB Output is correct
5 Correct 550 ms 785552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 518 ms 784504 KB Output is correct
2 Correct 540 ms 784504 KB Output is correct
3 Correct 638 ms 804032 KB Output is correct
4 Correct 598 ms 791476 KB Output is correct
5 Correct 550 ms 785552 KB Output is correct
6 Correct 488 ms 784300 KB Output is correct
7 Correct 630 ms 798072 KB Output is correct
8 Correct 471 ms 784248 KB Output is correct
9 Correct 479 ms 784356 KB Output is correct
10 Correct 487 ms 784376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 403 ms 783736 KB Output is correct
2 Correct 401 ms 783608 KB Output is correct
3 Correct 418 ms 783864 KB Output is correct
4 Correct 434 ms 783736 KB Output is correct
5 Correct 409 ms 783608 KB Output is correct
6 Correct 518 ms 784504 KB Output is correct
7 Correct 540 ms 784504 KB Output is correct
8 Correct 638 ms 804032 KB Output is correct
9 Correct 598 ms 791476 KB Output is correct
10 Correct 550 ms 785552 KB Output is correct
11 Correct 488 ms 784300 KB Output is correct
12 Correct 630 ms 798072 KB Output is correct
13 Correct 471 ms 784248 KB Output is correct
14 Correct 479 ms 784356 KB Output is correct
15 Correct 487 ms 784376 KB Output is correct
16 Correct 1817 ms 786940 KB Output is correct
17 Execution timed out 3119 ms 795556 KB Time limit exceeded
18 Halted 0 ms 0 KB -