2599: [ioi2011]race time limit:50 Sec Memory limit:128 MB
submit:1401 solved:412
[Submit] [Status] Description
To a tree, each side of the right. Find a path, weights and equals K, and the number of edges is the smallest.
Input
First row of two integers n, k
Second.. n rows three integers per line represents the ends and weights of a non-forward edge (note numbering starts with 0)
Output
An integer representing the minimum number of edges if there is no such path output-1
Sample Input4 3
0 1 1
1 2 2
1 3 4
Sample Output
2
Exercises
Actually 1 A, still running so fast 233 ....
On the tree path we can consider point division.
If we are currently dealing with a tree with RT as root, then the trouble with the path statistics is the two points within the same subtrees tree. Use g[i] to indicate the minimum number of edges to reach I distance.
To deal with this problem is simple, we can use a subtrees tree to update the answer, and then use its information to update our G array.
That's it, and finally do it again.
InlinevoidWorkintx) {V[x]=1; for(intI=head[x],y;i;i=e[i].next)if(!v[y=E[i].go]) {CNT=0; GETDEP (Y,X,E[I].W,1); For1 (j,cnt)if(d[j][0]<=k) Ans=min (ans,g[k-d[j][0]]+d[j][1]); For1 (j,cnt)if(d[j][0]<=K) g[d[j][0]]=min (g[d[j][0]],d[j][1]); } ans=min (ans,g[k]); for(intI=head[x],y;i;i=e[i].next)if(!v[y=E[i].go]) {CNT=0; GETDEP (Y,X,E[I].W,1); For1 (j,cnt)if(d[j][0]<=K) g[d[j][0]]=inf; } for(intI=head[x],y;i;i=e[i].next)if(!v[y=E[i].go]) {Sum=s[y];rt=0; GETRT (Y,X); Work (RT); }}
Code:
1#include <cstdio>2 3#include <cstdlib>4 5#include <cmath>6 7#include <cstring>8 9#include <algorithm>Ten One#include <iostream> A -#include <vector> - the#include <map> - -#include <Set> - +#include <queue> - +#include <string> A at #defineINF 1000000000 - - #defineMAXN 200000+5 - - #defineMAXM 2000000+5 - in #defineEPS 1e-10 - to #definell Long Long + - #definePA pair<int,int> the * #defineFor0 (i,n) for (int i=0;i<= (n); i++) $ Panax Notoginseng #defineFor1 (i,n) for (int i=1;i<= (n); i++) - the #defineFor2 (i,x,y) for (int i= (x); i<= (y); i++) + A #defineFor3 (i,x,y) for (int i= (x); i>= (y); i--) the + #defineMoD 1000000007 - $ using namespacestd; $ -InlineintRead () - the { - Wuyi intx=0, f=1;CharCh=GetChar (); the - while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} Wu - while(ch>='0'&&ch<='9') {x=Ten*x+ch-'0'; ch=GetChar ();} About $ returnx*F; - - } - structedge{intGo,next,w;} e[2*MAXN]; A intn,k,cnt,tot,sum,rt,ans=inf,f[maxn],g[maxm],d[maxn][2],HEAD[MAXN],S[MAXN]; + BOOLV[MAXN]; theInlinevoidInsertintXintYintz) - { $E[++tot]= (Edge) {y,head[x],z};head[x]=tot; theE[++tot]= (Edge) {x,head[y],z};head[y]=tot; the } theInlinevoidGETRT (intXintFA) the { -s[x]=1; f[x]=0; in for(intI=head[x],y;i;i=e[i].next)if(!v[y=e[i].go]&&y!=FA) the { the getrt (y,x); Abouts[x]+=S[y]; thef[x]=Max (f[x],s[y]); the } theF[x]=max (f[x],sum-f[x]); + if(F[x]<f[rt]) rt=x; - } theInlinevoidGETDEP (intXintFaintW1,intW2)Bayi { thed[++cnt][0]=w1;d[cnt][1]=W2; the for(intI=head[x],y;i;i=e[i].next)if(!V[Y=E[I].GO]&&Y!=FA) GETDEP (y,x,w1+e[i].w,w2+1); - } -InlinevoidWorkintx) the { thev[x]=1; the for(intI=head[x],y;i;i=e[i].next)if(!v[y=E[i].go]) the { -Cnt=0; theGETDEP (Y,X,E[I].W,1); theFor1 (J,CNT)if(d[j][0]<=k) Ans=min (ans,g[k-d[j][0]]+d[j][1]); theFor1 (J,CNT)if(d[j][0]<=K) g[d[j][0]]=min (g[d[j][0]],d[j][1]);94 } theans=min (ans,g[k]); the for(intI=head[x],y;i;i=e[i].next)if(!v[y=E[i].go]) the {98Cnt=0; AboutGETDEP (Y,X,E[I].W,1); -For1 (J,CNT)if(d[j][0]<=K) g[d[j][0]]=inf;101 }102 for(intI=head[x],y;i;i=e[i].next)if(!v[y=E[i].go])103 {104sum=s[y];rt=0; the getrt (y,x);106 Work (RT);107 }108 }109 the intMain ()111 the {113 theFreopen ("Input.txt","R", stdin); the theFreopen ("output.txt","W", stdout);117 118N=read (); k=read ();119For0 (i,k) g[i]=inf; -For1 (i,n-1)121 {122 intX=read () +1, Y=read () +1, z=read (); Insert (x, y, z);123 }124f[rt=0]=inf; thesum=N;126GETRT (1,0);127 Work (RT); - if(ans>=n) ans=-1;129cout<<ans<<Endl; the 131 return 0; the 133}
View Code
BZOJ2599: [Ioi2011]race