Travel time limit for previous questions ministers: 1.0s memory limit: 256.0MBProblem description
Long ago, the Kingdom of T was prosperous. In order to manage the country better, the kingdom has built a lot of fast roads to connect the capitals and the major cities in the kingdom.
To save money, the country's ministers have thought through a set of excellent construction plans that will allow any big city to reach directly from the capital or through other big cities. At the same time, the plan to reach every major city from the capital is unique if it is not repeated through big cities.
J is the important minister of T country, he inspected between the major cities, to observe the public sentiment. So, from one city to another, it's the most common thing J does. He has a purse to store the tolls between the cities.
Smart J found that if not stopped in a city to repair, in the course of continuous travel, he spent the tolls and the distance he has travelled, in the X-kilometer to x+1 km 1-kilometer (x is an integer), he spent the tolls is x+10 so much. That means walk 1-kilometer cost 11, walk 2-kilometer to spend 23.
Secretary J wants to know: He departs from one city, does not rest, and arrives in another city, what is the maximum amount of travel expenses that may be spent?
Input format
The first line of input contains an integer n, representing the number of cities in the T kingdom, including the capital
The city is numbered starting from 1 and the city of 1th is the capital.
Next n-1 line, describe the highway of T Country (the highway of T country must be n-1 strip)
Three integers per line pi, qi, Di, indicates a high-speed road between the city Pi and the city Qi, with a length of Di km.
Output format
Output An integer that indicates how much travel expenses the minister of J spends.
Sample Input 15
1 2 2
1 3 1
2 4 5
2 5 4 Sample output 1135 output format
Minister J from the City 4 to the city 5 costs 135 of the tolls.
Solution One:
Tree diameter problem: Two BFS (proved by the counter-proof method)
1#include <iostream>2#include <cmath>3#include <cstdlib>4#include <cstdio>5#include <cstring>6#include <vector>7#include <queue>8 using namespacestd;9 structedge{Ten intTo,next,val; One }; A Long LongMax_len; - intMax_num,n; - voidBFsintSLong Long*dis,edge *e,int*h) { the //memset (dis,-1,sizeof (dis)); - inttop,i; - for(i=1; i<=n;i++){ -dis[i]=-1; + } -queue<int>Q; + Q.push (s); A //vis[s]=true; atdis[s]=0; - - while(!Q.empty ()) { - - //cout<<max_num<<endl; - intop=Q.front (); - Q.pop (); to //Vis[top]=false; + for(i=h[top];i+1; i=E[i].next) { - //cout<<i<<endl; the if(dis[e[i].to]==-1){ * //vis[e[i].to]=true; $dis[e[i].to]=dis[top]+E[i].val;Panax Notoginseng if(dis[e[i].to]>Max_len) { -max_len=dis[e[i].to]; themax_num=e[i].to; + } A Q.push (e[i].to); the } + } - } $ } $ intMain () {//the diameter of the tree - //freopen ("D://INPUT.txt "," R ", stdin); -scanf"%d",&n); theEdge *e=Newedge[n*2+1]; - int*h=New int[n+1];Wuyi Long Long*dis=New Long Long[n+1]; the //bool *vis=new vis[n+1]; - inti,num=0; Wu intnn=n-1, U,v,val; - //memset (h,-1,sizeof (h)); About for(i=1; i<=n;i++){ $h[i]=-1; - } - - A for(i=0; i<nn;i++) {//Build adjacency Table +scanf" %d%d%d",&u,&v,&val); thee[num].to=v; -E[num].val=Val; $e[num].next=H[u]; theh[u]=num++; the thee[num].to=u; theE[num].val=Val; -e[num].next=H[v]; inh[v]=num++; the } themax_len=-1; Aboutmax_num=1; theBFs1, dis,e,h); the the //cout<<max_num<<endl; + //cout<<max_len<<endl; - the BFS (max_num,dis,e,h);Bayicout<<max_len* (max_len+ +)/2<<Endl; the Delete[]e; the Delete[]h; - Delete[]dis; - return 0; the}
Blue Bridge cup The travel expenses of the ministers of previous examinations