Alice's Journey
Practice
3.8 (5 votes)
Depth first search
Algorithms
Graphs
Problem
81% Success 411 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Alice has a weighted tree of \(N\) vertices. A tree is a connected acyclic graph with exactly \(N - 1\) edges.

Alice wants to visit all the vertices of the tree by following the edges that connect these vertices. But she wants to do so in minimum time. She can pick any tree vertex as the starting point of her journey. Let’s say her current position is \(C\). Then she can move to any adjacent vertex of \(C\), but it costs her time equal to the weight of the edge. She will continue the above moves until she visits all the vertices.

Calculate the shortest time when Alice can visit all the vertices if she can choose any vertex as her starting point.

Input Format

The first line contains a single integer \(N\) denoting the number of vertices in the tree.

The next \(N - 1\) lines contain 3 space-separated integers \((u, v, w)\), denoting an edge between vertices \(u\) and \(v\) of weight \(w\).

Output Format

Print a single integer denoting Alice's shortest time to visit all the vertices if she can choose any vertex as her starting point.

Constraints

\(1 ≤ N ≤ 2 * 10^5\)

\(1 ≤ u, v ≤ N\)

\(1 ≤ w ≤ 10^4\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
2 votes
Tags:
GraphsAlgorithmsDepth First SearchC++
Points:30
3 votes
Tags:
AlgorithmsDepth First SearchGraphsC++
Points:30
4 votes
Tags:
GraphsAlgorithmsDepth First Search