Algorithmic note _191: Travel of successive Test secretaries (Java)

Source: Internet
Author: User

Directory

1 Problem Description

2 Solutions

  1 problem description 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 4Sample Output 1135output Format

Minister J from the City 4 to the city 5 costs 135 of the tolls.

2 Solutions

The specific code is as follows:

Importjava.util.ArrayList;ImportJava.util.Scanner; Public classMain { Public Static intN;  Public Static intMax =Integer.min_value;  Public Static intPoint = 0;  Public StaticArraylist<edge>[] map; Static classEdge { Public intP;  Public intQ;  Public intD;  PublicEdgeintPintQintD) { This. P =P;  This. Q =Q;  This. BD; }    }         Public voidDfsintStartBoolean[] visited,intdis) {Visited[start]=true;  for(inti = 0;i < Map[start].size (); i++) {Edge temp=Map[start].get (i); if(Visited[temp. Q] = =false) {dis+=temp.                D if(Dis >Max) {Max=dis; Point=temp.                Q; } DFS (temp.                Q, visited, dis); Dis-=temp.            D }        }    }         Public voidGetResult () {Boolean[] visited =New Boolean[n + 1]; DFS (1, visited, 0); Visited=New Boolean[n + 1]; intStart =Point ; DFS (start, visited,0); intresult = Max * + (max + 1) * MAX/2;    SYSTEM.OUT.PRINTLN (result); } @SuppressWarnings ("Unchecked")     Public Static voidMain (string[] args) {main test=NewMain (); Scanner in=NewScanner (system.in); N=In.nextint (); Map=NewArraylist[n + 1];  for(inti = 1;i <= n;i++) Map[i]=NewArraylist<edge>();  for(inti = 1;i < n;i++) {            intP =In.nextint (); intQ =In.nextint (); intBIn.nextint (); Map[p].add (NewEdge (P, Q, D)); Map[q].add (NewEdge (Q, P, D));    } test.getresult (); }}

Algorithmic note _191: Travel of successive Test secretaries (Java)

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.