Travel of the Minister (diameter of the tree)

Source: Internet
Author: User

Problem 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: The maximum distance of two nodes of a tree, that is, the diameter of the tree.

The diameter of a tree refers to the longest and simplest path of a tree. Method: two times BFS: first select a starting point BFS find the end of the longest road, and then from the end of the BFS, then the second BFS found the longest road is the diameter of the tree;

          principle: Set start from U, first BFS (DFS) found end point v must be one endpoint of the diameter of the tree
          Proof: 1) if u is a point on a diameter, then V is obviously the end of the diameter (because if V is not, then there must be another point w so that the distance between U and W is longer, then the BFS found v contradiction)
                 2) if u is not a point on a diameter, then u to v must intersect (disprove) the diameter of the tree, then the intersection to V must be the second half of the diameter

So v must be an endpoint of the diameter, so the BFS (DFS) from V is bound to have a diameter length

The array must be large enough to open.

#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include < Algorithm> #include <stack>using namespace std; #define MAX 1<<30#define maxn 1000100struct node{int V,len    ;        Node (int v_,int len_) {v=v_;    Len=len_;    }};vector <Node> g[maxn];int n;int vis[maxn];int dis[maxn];void dfs (int u) {vis[u]=1;    int m=g[u].size ();        for (int i=0;i<m;i++) {int v=g[u][i].v;            if (!vis[v]) {Dis[v]=dis[u]+g[u][i].len;        DFS (v);        }}}int Main () {while (scanf ("%d", &n)!=eof) {for (int i=1;i<=n;i++) g[i].clear ();            for (int i=1;i<n;i++) {int x,y,len;            scanf ("%d%d%d", &x,&y,&len);            G[x].push_back (Node (Y,len));        G[y].push_back (Node (X,len));        } memset (Vis,0,sizeof (VIS));        memset (dis,0,sizeof (dis));        DFS (1);        int Key=1;        int maxlen=0; for (int i=1;i<=n;i++) {            if (Maxlen<dis[i]) {maxlen=dis[i];            Key=i;        }} memset (Vis,0,sizeof (VIS));        memset (dis,0,sizeof (dis));        DFS (key);        maxlen=0;                for (int i=1;i<=n;i++) {if (Maxlen<dis[i]) {maxlen=dis[i];            Key=i;    }} printf ("%d\n", (Maxlen*maxlen+maxlen)/2+10*maxlen); } return 0;}


Travel of the Minister (diameter of the tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.