Submission #236483

#TimeUsernameProblemLanguageResultExecution timeMemory
236483MohamedAhmed04Museum (CEOI17_museum)C++14
100 / 100
399 ms181032 KiB
#include <bits/stdc++.h>

using namespace std ;

const int MAX = 1e4 + 2 ;

int n , k , x ;

vector< vector< pair<int , int> > >adj(MAX) ;

int sz[MAX] ;
int dp[MAX][MAX][2] , tmp[MAX][2] ;

void dfs(int node , int par)
{
	dp[node][1][0] = dp[node][1][1] = 0 ;
	sz[node] = 1 ;
	for(auto &childd : adj[node])
	{
		int child = childd.first , cost = childd.second ;
		if(child == par)
			continue ;
		dfs(child , node) ;
		for(int k1 = 0 ; k1 <= min(sz[node] , k) ; ++k1)
		{
			for(int k2 = 0 ; k2 <= sz[child] && k1 + k2 <= k ; ++k2)
			{
				tmp[k1+k2][0] = min(tmp[k1+k2][0] , dp[node][k1][0] + dp[child][k2][0] + (k2 > 0) * 2 * cost) ;
				tmp[k1+k2][1] = min(tmp[k1+k2][1] , dp[node][k1][0] + dp[child][k2][1] + (k2 > 0) * cost) ;
				tmp[k1+k2][1] = min(tmp[k1+k2][1] , dp[node][k1][1] + dp[child][k2][0] + (k2 > 0) * 2 * cost) ;
			}
		}
		sz[node] += sz[child] ;
		for(int i = 1 ; i <= min(sz[node] , k) ; ++i)
		{
			dp[node][i][0] = tmp[i][0] ;
			dp[node][i][1] = tmp[i][1] ;
			tmp[i][0] = tmp[i][1] = 1e9 ;
		}
	}
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n>>k>>x ;
	for(int i = 0 ; i < n-1 ; ++i)
	{
		int x , y , z ;
		cin>>x>>y>>z ;
		adj[x].emplace_back(y , z) ;
		adj[y].emplace_back(x , z) ;
	}
	for(int i = 0 ; i <= k ; ++i)
		tmp[i][0] = tmp[i][1] = 1e9 ;
	dfs(x , -1) ;
	return cout<<dp[x][k][1]<<"\n" , 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...