Add One Zero-Weight Edge to Minimize the Sum of Root Distances
Time limit2sMemory limit1024 MB
Given a weighted tree rooted at vertex 1, add one zero-weight edge to minimize the sum of root distances and print that minimum.
- Level
Hard8 of 10
- Topics
- Tree, DFS, Dynamic programming
- Solved
- No attempts yet
문제
A tree is a simple connected graph with no cycles.
You are given a tree with vertices. The vertices are numbered from 1 to . The root of the tree is always vertex 1, and every edge has a positive weight.
You may use the following operation at most once.
- Add an edge of weight between any two vertices and .
Let be the shortest distance from vertex to the root vertex. Using the operation exactly once, write a program that minimizes .
입력
The first line contains the number of vertices .
Each of the next lines contains three integers , , and , separated by spaces, describing an edge between and with weight .
출력
Print the minimum possible value of .