Minister's travel expenses (deep search)

Problem Description

A long time ago, the T Kingdom prospered unprecedentedly. 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 developed a set of excellent construction plans after thinking, so that any big city can be reached directly from the capital or indirectly through other big cities. At the same time, the plan to reach each big city from the capital is unique if it does not pass through the big cities repeatedly.

J is an important minister of country T. He patrols the major cities and observes the public sentiment. So, going from one city to another without stopping has become J's most common thing to do. He has a purse to store travel expenses between cities.

Smart J found that if he did not stop for repairs in a certain city, in the process of continuous travel, the toll he spent was related to the distance he had traveled. In (x is an integer), he spends as much as x+10. That is to say, it costs 11 to walk 1 km, and 23 to walk 2 km.

Minister J would like to know: What is the maximum travel expenses he may spend from a certain city to another city without rest in between?
input format

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

Cities are numbered sequentially from 1, with city number 1 being the capital.

The next line n-1 describes the expressway in country T (the expressway in country T must be n-1)

There are three integers Pi, Qi, Di in each line, indicating that there is a highway between the city Pi and the city Qi, and the length is Di kilometers.
output format

Output an integer indicating how much the minister J has to spend at most.
Sample Input 1
5
1 2 2
1 3 1
2 4 5
2 5 4
Sample Output 1
135
Output Format

Minister J costs 135 to travel from city 4 to city 5.

The solution
code is as follows:

import java.util.Arrays;
import java.util.Scanner;

/** 
 * @author 作者 : Cactus
 * @version 创建时间:2018-3-28 下午23:52:32 
 */
//tolls = ((11 + 10 +max)*max)/2
public class Main {
    private static int n, max = -9999, rightStart;
    //rightStart 最长路径的一个端点
    //max 记录最长的路径长度
    private static int[][] road; //存放路径及权值
    private static int[] visited;// 标记
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        String drop = sc.nextLine();
        road = new int[n + 1][n + 1];
        visited = new int[n + 1];
        int pi, qi, di;
        for(int i = 0; i < n - 1; i++){
            String[] arr = sc.nextLine().split(" ");
            pi = Integer.valueOf(arr[0]);
            qi = Integer.valueOf(arr[1]);
            di = Integer.valueOf(arr[2]);
            road[pi][qi] = di;
            road[qi][pi] = di;
        }
        sc.close();
        //查找最长路径的端点
        visited[1] = 1;
        dfs(1,0);
        Arrays.fill(visited, 0);
        //得到最长路径的长度
        visited[rightStart] = 1;
        dfs(rightStart,0);
        //计算旅费
        System.out.println(((11 + 10 + max) * max) / 2);
    }
    private static void dfs(int x, int dist){ //深搜
        if(max < dist){
            rightStart = x;
            max = dist;
        }
        for(int i = 1; i <= n; i++){
                if(visited[i] != 1 && road[x][i] != 0){
                    visited[i] = 1;
                    dist += road[x][i];
                    dfs(i, dist);
                    visited[i] = 0;
                    dist -= road[x][i];
            }
        }
        return;
    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325518569&siteId=291194637