Floyd+ Enumeration
Watch:
1.floyd at the same time with the array P record transfer node K, so that the end of the line point U v can get the entire line segment
2. Any point C to the distance of line a B = (d[a][c]+d[c][b]-d[a][b])/2
3. Enumerate all sub-segments of the diameter St en B:
for (int a=en;a;a=p[st][a])
for (int b=a;b;b=p[st][b])
Code:
#include <iostream>#include<cstring>#defineSize 305using namespacestd;intn,s;intnum=0;intans=0x3f3f3f3f;intD[size][size];intP[size][size];structpath{intU,v;} G[size*Size];intCnt=0;intMain () {memset (d,0x3f,sizeof(d)); CIN>>n>>s; for(intI=1; i<=n;i++) d[i][i]=0; inta,b,w; for(intI=1; i<n;i++) {cin>>a>>b>>W; D[A][B]=d[b][a]=W; P[A][B]=a; p[b][a]=b; } for(intk=1; k<=n;k++){ for(intI=1; i<=n;i++){ if(k==i)Continue; for(intj=1; j<=n;j++){ if(k==j| | I==J)Continue; if(d[i][k]+d[k][j]<D[i][j]) {D[i][j]=d[i][k]+D[k][j]; P[I][J]=K; Num=Max (num,d[i][j]); } } } } for(intI=1; i<=n;i++){ for(intj=1; j<=n;j++){ if(d[i][j]==num) {g[++cnt].u=i; g[cnt].v=J; } } } for(intI=1; i<=cnt;i++){ intst=g[i].u;inten=g[i].v; //cout<<st<< ' <<en<<endl; for(intA=en;a;a=P[st][a]) { for(intb=a;b;b=P[st][b]) { if(D[a][b]>s) Break; //cout<< "" <<a<< "<<b<<" "<<endl; intdis=0; for(intC=1; c<=n;c++) {Dis=max (DIS, (d[a][c]+d[c][b]-d[a][b])/2);} Ans=min (Ans,dis); } }} cout<<ans<<Endl;}
The nucleus of the code1167 tree network