1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #include <cstdio> #include <vector> #include <cstring> #define F(n) Fi(i,n) #define Fi(i,n) for(int i=0;i<n;i++) #define N 100010 using namespace std; struct MP{ int t,c; }F[N]; int ans,ST[5]; bool wed[N]; bool gin(int &a){ char c; while(c=getchar(),c<'0'||c>'9')if(c==-1)return 0; a=c-'0'; while(c=getchar(),c>='0'&&c<='9')a=a*10+c-'0'; return 1; } MP DFS(vector<MP>*G,int now,int no){ wed[now]=1; MP drv=(MP){now,0}; F[now]=drv; for(MP p:G[now])if(p.t!=no){ MP tmp=DFS(G,p.t,now); tmp.c+=p.c; if(tmp.c>drv.c)drv=tmp,F[now]=p; } return drv; } int main(){ int n,m,l,a,b,c; while(gin(n),gin(m),gin(l)){ vector<MP>G[N]; ans=0; memset(ST,0,sizeof(ST)); memset(wed,0,sizeof(wed)); F(m){ gin(a);gin(b);gin(c); G[a].push_back((MP){b,c}); G[b].push_back((MP){a,c}); } F(n)if(!wed[i]){ int str=DFS(G,i,-1).t; int tmp=DFS(G,str,-1).c; ans=max(tmp,ans); int sum=0,st=3; if(tmp)while(sum+F[str].c<=tmp/2)sum+=F[str].c,str=F[str].t; tmp=min(sum+F[str].c,tmp-sum); while(st&&tmp>ST[st-1])ST[st]=ST[st-1],st--; ST[st]=tmp; } if(n-m>2)ans=max(ans,max(ST[0]+l+ST[1],ST[1]+ST[2]+l*2)); else if(n-m>1)ans=max(ans,ST[0]+l+ST[1]); printf("%d\n",ans); } }
|