MIT Tour

시간 제한3초메모리 제한256 MB

요약
1번 방을 루트로 하는 가중치 트리에서 각 레벨마다 방 하나씩을 고르되 연속한 두 방이 간선으로 연결되지 않도록 하면서, 이동 거리의 합을 최소로 만든다.
난이도

어려움10점 중 8점

유형
트리, 동적 계획법, DFS, 최단 경로
정답자
아직 제출이 없습니다

문제

Busy Beaver is visiting MIT, so he decided to have a tour around campus.

The campus consists of NN rooms and N−1N-1 corridors between them. The rooms are enumerated consecutively from 11 to NN, and the ii-th corridor has a known length w_iw\_i and can be traversed in both directions. The layout of campus is also such that between any two rooms, there is exactly one way to go from one to another through these corridors without going through any room twice. Define the level of a room to be the minimum number of corridors needed to get from the Great Dome (room 11) to this room. For instance, the Great Dome is level 00, and any room directly connected to it would be level 11. Let kk be the highest level of any room on campus, and denote d(u,v)d(u, v) to be the length of the shortest path between rooms u,vu, v.

Busy Beaver wants his tour to be interesting. Starting from the Great Dome (room 11), he wishes to visit a sequence of rooms a_1,…,a_ka\_1, \dots, a\_k such that for each 1≤i≤k1 \leq i \leq k, room a_ia\_i is on level ii, and for each 1≤i<k1 \leq i < k, rooms a_i,a_i+1a\_i, a\_{i+1} are not connected by a corridor. As Busy Beaver is busy as usual, he also wants to minimize the total distance of the tour, which is d(1,a_1)+d(a_1,a_2)+⋯+d(a_k−1,a_k).d(1, a\_1) + d(a\_1, a\_2) + \dots + d(a\_{k-1}, a\_k).

Since the MIT campus is very complex, Busy Beaver needs your help to plan out his tour. Help him find out the minimum length of an interesting tour.

입력

The first line consists of a single integer NN (2≤N≤2⋅1052 \le N \le 2 \cdot 10^5) --- the number of rooms at the MIT campus.

Each of the next N−1N-1 lines contains three integers uu, vv, ww (1≤u,v≤N1 \le u,v \le N, u≠vu \neq v, 1≤w≤1071 \le w \le 10^7) --- a corridor between rooms uu and vv with the length ww.

It is guaranteed that there is exactly one simple path between any two rooms, and that at least one interesting tour exists.

출력

Output a single integer, the minimal distance of an interesting MIT tour.

힌트

In the first test case, there are two possible interesting tours:

  • \[a_1]=\[2]\[a\_1] = \[2] --- The total distance of the tour will be 11.
  • \[a_1]=\[3]\[a\_1] = \[3] --- The total distance of the tour will be 11.

They both obtain the minimum possible length of 11, which is the answer for this case.

In the second test case, notice that both rooms of level 22 are connected to room 33. Therefore, no interesting tour can have a_1=3a\_1 = 3, or otherwise it will violate the condition that a_1,a_2a\_1, a\_2 are not connected by a corridor. Therefore, there are two possible interesting tours:

  • \[a_1,a_2]=\[2,4]\[a\_1, a\_2] = \[2, 4] --- The tour will traverse edges 1→2→1→3→41\rightarrow 2\rightarrow 1\rightarrow 3\rightarrow 4. The total distance is d(1,a_1)+d(a_1,a_2)=3+6=9.d(1, a\_1) + d(a\_1, a\_2) = 3 + 6 = 9.
  • \[a_1,a_2]=\[2,5]\[a\_1, a\_2] = \[2, 5] --- The tour will traverse edges 1→2→1→3→51\rightarrow 2\rightarrow 1\rightarrow 3\rightarrow 5. The total distance is d(1,a_1)+d(a_1,a_2)=3+7=10.d(1, a\_1) + d(a\_1, a\_2) = 3 + 7 = 10.

Therefore, the minimum length of an interesting tour in this case is 99.

예제2

  1. 예제 1

    입력
    3
    1 2 1
    1 3 1
    
    예상 출력
    1
    
  2. 예제 2

    입력
    5
    1 2 3
    1 3 1
    3 4 2
    3 5 3
    
    예상 출력
    9