代码:
#include<stdio.h> #include<string.h> #include<algorithm> #define N 210000 #define pr pair<int,int> using namespace std; int next[N<<1],to[N<<1],val[N<<1],head[N],ce; pr st[N]; int hash[1000100],K,ans,top; bool ban[N]; void add(int x,int y,int v) { to[++ce]=y; val[ce]=v; next[ce]=head[x]; head[x]=ce; } int getS(int x,int pre) { int size=1,i; for(i=head[x];i;i=next[i]) { if(to[i]!=pre&&!ban[to[i]]) { size+=getS(to[i],x); } } return size; } int getG(int x,int pre,int tot,int &g) { int size=1,temp,i; bool flag=1; for(i=head[x];i;i=next[i]) { if(to[i]!=pre&&!ban[to[i]]) { temp=getG(to[i],x,tot,g); size+=temp; if(temp>(tot>>1)) flag=0; } } if(tot-size>(tot>>1)) flag=0; if(flag) g=x; return size; } void getP(int x,int pre,int dis,int cnt) { st[++top]=pr(dis,cnt); for(int i=head[x];i;i=next[i]) { if(to[i]!=pre&&!ban[to[i]]) { getP(to[i],x,dis+val[i],cnt+1); } } } void calc(int x) { int temp,i,j; top=hash[0]=0; for(i=head[x];i;i=next[i]) { if(!ban[to[i]]) { temp=top; getP(to[i],0,val[i],1); for(j=temp+1;j<=top;j++) if(st[j].first<=K) ans=min(ans,hash[K-st[j].first]+st[j].second); for(j=temp+1;j<=top;j++) if(st[j].first<=K) hash[st[j].first]=min(hash[st[j].first],st[j].second); } } for(i=1;i<=top;i++) if(st[i].first<=K) hash[st[i].first]=0x3f3f3f3f; } void slove(int x,int tot) { ban[x]=1; if(tot==1) return ; calc(x); int g,i,s; for(i=head[x];i;i=next[i]) { if(!ban[to[i]]) { s=getS(to[i],0); getG(to[i],0,s,g); slove(g,s); } } } int main() { int n,m,i,j,x,y,v; scanf("%d%d",&n,&K); for(i=1;i<n;i++) { scanf("%d%d%d",&x,&y,&v); x++,y++; add(x,y,v),add(y,x,v); } memset(hash,0x3f,sizeof(hash)); getG(1,0,n,x); ans=0x3f3f3f3f; slove(x,n); if(ans==0x3f3f3f3f) ans=-1; printf("%d\n",ans); return 0; }
原文:http://blog.csdn.net/ww140142/article/details/49301611