Minister’s travel expenses [the diameter of the tree] [DFS]

Minister's travel expenses

Description

A long time ago, Kingdom T had an unprecedented prosperity. In order to better manage the country, the kingdom has built a large number of expressways to connect the capital and major cities in the kingdom.

In order to save money, the ministers of country T have formulated an excellent construction plan after thinking, so that any big city can be reached directly from the capital or indirectly through other big cities. At the same time, if you don't repeatedly pass through big cities, the plan from the capital to each big city is unique.

J is an important minister of country T. He patrols between major cities to appreciate the sentiments of the people. Therefore, non-stop from one city to another has become the most common thing J does. He has a purse to store travel expenses between cities.

Smart J found that if he does not stop to repair in a certain city, the travel expenses he spends during the continuous journey are related to the distance he has traveled, and he is walking the kilometer from the xth kilometer to the x+1th kilometer. Medium (x is an integer), his travel expenses are x+10 so much. In other words, it costs 11 to walk 1 kilometer, and 23 to walk 2 kilometers.

Minister J wants to know: What is the maximum amount of travel expenses that he might spend when starting from a certain city without taking a break and arriving in another city?

Input

The first line of input contains an integer n, representing the number of cities in Kingdom T including the capital

Cities are numbered sequentially starting from 1, and city 1 is the capital.

Next line n-1, describe the highway in country T (the highway in country T must be n-1)

Each line has three integers Pi, Qi, Di, indicating that there is a highway between the city Pi and the city Qi, and the length is Di kilometers.

Output

Output an integer, which indicates how much the minister J spent the most on the toll.

Sample Input 1 

5
1 2 2
1 3 1
2 4 5
2 5 4

Sample Output 1

135

Analysis: For an undirected graph, find the longest path between any two points. Another explanation is to find out the diameter of a tree for a tree.

How to find the diameter of the tree --> You need to find the start and end of the diameter, and then add the weights on the path. We start from any point X and find the longest path from X. The end of this path is the starting point S or the end point E of the diameter. 【It has been demonstrated】

In this way, the problem is solved. First, run DFS again, start with x = 1, find the longest path, and record the endpoint g. Run DFS again from point g, and the length to the end point e is the diameter of the tree.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <climits>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <bits/stdc++.h>
#include <queue>
#include <algorithm>
#include <map>
#include <cstdlib>
using namespace std;
#define inf 0x3f3f3f3f
const int N = 1000005;
struct node
{
    int to;
    int cost;
    node(int a, int b):to(a),cost(b) {}
};
vector<node>edge[N];
int g, maxl = -1;
bool vis[N];
void dfs(int s, int sum)
{
    vis[s] = 1;
    if(sum > maxl)
    {
        maxl = sum;
        g = s;
    }
    for(int i = 0; i < edge[s].size(); i ++)
    {
        node t = edge[s][i];
        if(!vis[t.to])
        {
            dfs(t.to,sum+t.cost);
        }
    }
}
int main()
{

    int n,u,v,w;
    scanf("%d",&n);
    for(int i = 0; i < n - 1; i ++)
    {
        scanf("%d %d %d", &u,&v,&w);
        edge[u].push_back(node(v,w));
        edge[v].push_back(node(u,w));
    }
    // 从任意一点出发,找到直径的一点。
    dfs(1,0);
//    printf("%d\n",maxl);
    memset(vis,0,sizeof(vis));
    dfs(g,0);
    int  res = 10 * maxl + (maxl + 1) * maxl * 1.0 / 2;
    printf("%d\n",res);
    return 0;
}

 

Guess you like

Origin blog.csdn.net/Mercury_Lc/article/details/107151999