#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#include <bits/stdc++.h>
using namespace std;
#define sqr 340
#define mp make_pair
#define mid (l+r)/2
#define le node * 2
#define ri node * 2 + 1
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define era erase
#define C continue
#define mem(dp,i) memset(dp,i,sizeof(dp))
#define mset multiset
#define all(x) x.begin(), x.end()
#define gc getchar_unlocked
typedef long long ll;
typedef short int si;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
typedef pair<double,ll>pdi;
const ll inf=1e18;
const ll mod=998244353;
const ld Pi=acos(-1) ;
int n , k , root ;
vpi v [10009] ;
int dp [10009][10009][2] , sz [10009] ;
int Min ( int x , int y ) {
x = ( x == -1 ? 1e9 : x ) ;
return min ( x , y ) ;
}
void dfs ( int node , int p ) {
sz [node] = 1 ;
for ( auto U : v [node] ) {
int u = U .fi ;
if ( u == p ) C ;
dfs ( u , node ) ;
sz [node] += sz [u] ;
}
int crnt = 1 ;
dp [node][1][0] = dp [node][1][1] = 0 ;
for ( auto U : v [node] ) {
int u = U .fi , cost = U .se ;
if ( u == p ) C ;
crnt += sz [u] ;
for ( int i = min ( k , crnt ) ; i >= 2 ; i -- ) {
for ( int j = 1 ; j <= min ( i - 1 , sz [u] ) ; j ++ ) {
if ( dp [node][i-j][0] != -1 ) {
dp [node][i][0] = Min ( dp [node][i][0] , dp [node][i-j][0] + dp [u][j][1] + cost * 2 ) ;
}
if ( dp [node][i-j][1] != -1 ) {
dp [node][i][1] = Min ( dp [node][i][1] , dp [node][i-j][1] + dp [u][j][1] + cost * 2 ) ;
dp [node][i][0] = Min ( dp [node][i][0] , dp [node][i-j][1] + dp [u][j][0] + cost ) ;
}
}
}
}
}
int main () {
cin >> n >> k >> root ;
root -- ;
for ( int i = 0 ; i < n - 1 ; i ++ ) {
int a , b , c ;
scanf ("%d%d%d",&a ,&b ,&c ) ;
a -- , b -- ;
v [a] .pb ( { b , c } ) ;
v [b] .pb ( { a , c } ) ;
}
for ( int i = 0 ; i < n ; i ++ ) {
for ( int j = 1 ; j <= k ; j ++ ) {
dp [i][j][0] = dp [i][j][1] = -1 ;
}
}
dfs ( root , root ) ;
cout << min ( dp [root][k][0] , dp [root][k][1] ) << endl ;
}
Compilation message
museum.cpp: In function 'int main()':
museum.cpp:74:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
74 | scanf ("%d%d%d",&a ,&b ,&c ) ;
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
1 ms |
768 KB |
Output is correct |
3 |
Correct |
1 ms |
640 KB |
Output is correct |
4 |
Correct |
1 ms |
640 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
50300 KB |
Output is correct |
2 |
Correct |
41 ms |
50304 KB |
Output is correct |
3 |
Correct |
65 ms |
50592 KB |
Output is correct |
4 |
Correct |
53 ms |
50296 KB |
Output is correct |
5 |
Correct |
49 ms |
50168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
50300 KB |
Output is correct |
2 |
Correct |
41 ms |
50304 KB |
Output is correct |
3 |
Correct |
65 ms |
50592 KB |
Output is correct |
4 |
Correct |
53 ms |
50296 KB |
Output is correct |
5 |
Correct |
49 ms |
50168 KB |
Output is correct |
6 |
Correct |
41 ms |
50176 KB |
Output is correct |
7 |
Correct |
65 ms |
50424 KB |
Output is correct |
8 |
Correct |
36 ms |
50304 KB |
Output is correct |
9 |
Correct |
39 ms |
50168 KB |
Output is correct |
10 |
Correct |
36 ms |
50304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
1 ms |
768 KB |
Output is correct |
3 |
Correct |
1 ms |
640 KB |
Output is correct |
4 |
Correct |
1 ms |
640 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
44 ms |
50300 KB |
Output is correct |
7 |
Correct |
41 ms |
50304 KB |
Output is correct |
8 |
Correct |
65 ms |
50592 KB |
Output is correct |
9 |
Correct |
53 ms |
50296 KB |
Output is correct |
10 |
Correct |
49 ms |
50168 KB |
Output is correct |
11 |
Correct |
41 ms |
50176 KB |
Output is correct |
12 |
Correct |
65 ms |
50424 KB |
Output is correct |
13 |
Correct |
36 ms |
50304 KB |
Output is correct |
14 |
Correct |
39 ms |
50168 KB |
Output is correct |
15 |
Correct |
36 ms |
50304 KB |
Output is correct |
16 |
Correct |
194 ms |
120568 KB |
Output is correct |
17 |
Correct |
930 ms |
433272 KB |
Output is correct |
18 |
Correct |
1418 ms |
120876 KB |
Output is correct |
19 |
Correct |
134 ms |
120752 KB |
Output is correct |
20 |
Correct |
189 ms |
120824 KB |
Output is correct |
21 |
Execution timed out |
3148 ms |
745720 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |