Http://www.lydsy.com/JudgeOnline/problem.php? Id = 2599
For a tree, each edge has the right. Find a path, the sum of the weight and K, and the number of edges is the smallest. (Will I tell you this is the question ?)
Bzoj's question is good! I like one question in one sentence.
The idea of this question is still to divide and conquer, similar to the two questions in the previous article, but it is converted into a minimum, not a count ,,,
After finding the center of gravity U, we only consider the child tree with u as the root. The child tree of u can be managed separately.
We only consider passing through the root path
From the first two questions, you can easily think of this method:
First, all the binary groups (W, DEP) of the current tree are searched, sorted, and scanned. However, note that two vertices may come from the same subtree, if you add information to the binary group to which the subtree belongs, it becomes a triple .... It is complicated to determine whether two elements meet the conditions and must satisfy three pieces of information .. However, I still wrote this method for a long time, and finally I thought about other methods.
Inspired by some previous tree-like DP, we merge one subtree, that is, find a vertex (W, DEP) in the current subtree ), find a vertex in the front subtree.
To the root of the weight and is the K-W, and the depth is the smallest, we will be counted as DP [k-W] Good, (this method is quite common in the problem of merging sub-trees ), then, we can update the DP array after each query. Note that the initialization of the DP array cannot be all cleared. Otherwise, we need to make a decisive tle ....
The data on this question should be well created ....
Code attached: The speed is a little slow. It has been running more than 17 s.
#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;const int maxn = 222222;struct Divided_Conquer { struct Edge{ int v , w; Edge(){} Edge(int v,int w): v(v),w(w) { } }; bool Del[maxn]; vector<Edge> E[maxn]; int N , K; int size[maxn] , opt[maxn] ,dp[1000010]; vector<int> tnode ;vector<pair<int,int> > now; void Dfs(int u,int f) { tnode.push_back(u); size[u] = 1; opt[u] = 0; for(vector<Edge>::iterator it = E[u].begin(); it != E[u].end(); it++) { if(!Del[it->v] && it->v != f) { Dfs(it->v,u); size[u] += size[it->v]; opt[u] = max(opt[u],size[it->v]); } } } int Get_Root(int u) { tnode.clear(); Dfs(u,-1); int mi = maxn , ans = -1; for(vector<int>::iterator it = tnode.begin(); it != tnode.end(); it++) { opt[*it] = max(opt[*it],size[u]-size[*it]) ; if(opt[*it] < mi) { mi = opt[*it]; ans = *it; } } return ans; } vector<int> all ; void Get_Dis(int u,int len,int dep,int fa) {now.push_back(make_pair(len,dep)); for(vector<Edge>::iterator it = E[u].begin(); it != E[u].end(); it++) { if(!Del[it->v] && it->v != fa) { Get_Dis(it->v,len+it->w,dep+1,u); } } }inline void Merge(vector<pair<int,int> > a){for(vector<pair<int,int> >::iterator it = a.begin(); it != a.end(); it++) {if(it->first<=K) {all.push_back(it->first);Update(dp[it->first],it->second);}}} void Solve(int u) { u = Get_Root(u); all.clear(); int nch = 0;dp[0] = 0; for(vector<Edge>::iterator it = E[u].begin(); it != E[u].end(); it++) { if(!Del[it->v]){nch ++;now.clear();Get_Dis(it->v,it->w,1,u);Calc(now);Merge(now);} }for(vector<int>::iterator it = all.begin(); it != all.end(); it++) {dp[*it] = -1;} Del[u] = true; for(vector<Edge>::iterator it = E[u].begin(); it != E[u].end(); it++) { if(!Del[it->v]) { Solve(it->v); } } } inline void Calc(vector<pair<int,int> >now){for(vector<pair<int,int> >::iterator it = now.begin(); it != now.end(); it++) {if(it->first <= K ) {if(dp[K-it->first] != -1) {Update(Ans,dp[K-it->first]+it->second);}}} } inline void Update(int &x,int cmp) { if(x==-1 || x > cmp) x = cmp; } void Init(){ int a,b,c; Ans = -1; scanf("%d%d",&N,&K);fill(dp,dp+K+1,-1); for(int i = 1; i < N; i++) { scanf("%d%d%d",&a,&b,&c); a++ ; b++; E[a].push_back(Edge(b,c)); E[b].push_back(Edge(a,c)); } } int Ans;}sol;int main(){ sol.Init(); sol.Solve(1); printf("%d\n",sol.Ans); return 0;}