//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
typedef pair<int, int> T;
const int oo = 1e9+7;
void solve(){
int n, k, st; cin >> n >> k >> st;
vector<vector<T>>g(n+1);
for (int i = 1; i<n; i++){
int a, b, c; cin >> a >> b >> c;
g[a].emplace_back(b, c);
g[b].emplace_back(a, c);
}
vector<int>sub(n+1);
vector<vector<int>>dp(n+1, vector<int>(k+2, oo));
vector<vector<int>>dp2(n+1, vector<int>(k+1, oo));
function<void(int, int)>dfs = [&](int v, int pa){
sub[v] = 1;
dp[v][0] = 0;
dp[v][1] = 0;
dp2[v][0] = 0;
dp2[v][1] = 0;
for (auto [x, c]: g[v]){
if (x == pa) continue;
dfs(x, v);
for (int i = sub[v]; i>=0; i--){ //od tylu, by nie robic overcounting
for (int j = sub[x]; j>=0; j--){
if (i + j > k) continue;
dp[v][i+j] = min(dp[v][i+j], dp[v][i] + dp[x][j] + 2 * c);
dp2[v][i+j] = min(dp2[v][i+j], dp[v][i] + dp2[x][j]+ c);
//1. wchodzimy do jakichs poddrzew dzieci v, zawracamy i wchodzimy do jakiegos poddrzewa x znowu
//2. wchodzimy do jakichs poddrzew dzieci x, wchodzimy do v, pozniej do x i nie wracamy juz z v
}
}
sub[v] += sub[x];
}
};
dfs(st, st);
cout << min(dp[st][k], dp2[st][k]) << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
58 ms |
9756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
58 ms |
9756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |