Skip to content

Instantly share code, notes, and snippets.

@IvanIsCoding
Created December 7, 2017 19:08
Show Gist options
  • Select an option

  • Save IvanIsCoding/c5e53bac446c98bc751a2f31c1d2e0fb to your computer and use it in GitHub Desktop.

Select an option

Save IvanIsCoding/c5e53bac446c98bc751a2f31c1d2e0fb to your computer and use it in GitHub Desktop.
Seletiva IOI 2017
// Ivan Carvalho
// Tribo- Seletiva IOI - OBI 2017
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
const ll INF = 1e18;
const int MAXN = 1e4 + 10;
const int MAXK = 51;
ll melhor,N,K;
int marcado[MAXN];
vector<ii> grafo[MAXN];
ll dp[MAXN][MAXK];
void dfs(int v,int p){
for(int k = 0;k<=K;k++) dp[v][k] = INF;
dp[v][1] = dp[v][0] = 0;
ll temp[MAXK];
for(int i = 0;i<grafo[v].size();i++){
int u = grafo[v][i].first;
ll peso = grafo[v][i].second;
if(u == p) continue;
dfs(u,v);
for(int j = 0;j<=K;j++) temp[j] = dp[v][j];
for(int j = 0;j<=K;j++){
ll adiciona = j;
ll custo = peso + dp[u][j];
for(ll perdi = 0;perdi + adiciona <= K;perdi++){
temp[perdi + adiciona] = min(temp[perdi + adiciona], dp[v][perdi] + custo );
}
}
for(int j =0;j<=K;j++) dp[v][j] = temp[j];
}
melhor = min(melhor, dp[v][K] );
}
int main(){
cin >> N >> K;
for(int i = 1;i<N;i++){
ll u,v,w;
cin >> u >> v >> w;
grafo[u].push_back(ii(v,w));
grafo[v].push_back(ii(u,w));
}
melhor = 1e18;
dfs(1,0);
cout << melhor << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment