#P6670. Soda Travel Optimization

    ID: 19878 Type: Default 1000ms 256MiB

Soda Travel Optimization

Soda Travel Optimization

In this problem, there is a country with \(n\) cities connected by \(n-1\) roads forming a tree. Every road \(i\) has an associated soda consumption value \(w_i\). When traveling along a road, Niuniu drinks \(w_i\) amount of soda. Niuniu wants to plan a trip by choosing a starting city and then, on each day, traveling to a new city (i.e. along an unvisited road) until he stops at some city. Thus, his trip is a simple path in the tree (with at least one road).

The average soda consumption per day for a trip is \(P = \frac{S}{d}\), where \(S\) is the total soda consumed along the roads in the path and \(d\) is the number of days (roads) used on the trip. Given a positive integer \(k\), Niuniu wants the value \(|P-k|\) to be as small as possible. Your task is to help him design a travel plan such that the value of \(|P-k|\) is minimized, and then output this minimum value. If a trip produces an average that exactly equals \(k\), then the answer is \(0\).

Note: It is guaranteed that any two cities are connected by exactly one simple path.

inputFormat

The first line contains two integers \(n\) and \(k\) (where \(n\) is the number of cities and \(k\) the target average soda consumption per day).

Then \(n-1\) lines follow. Each line contains three integers \(u\), \(v\), and \(w\), indicating a road connecting cities \(u\) and \(v\) with soda consumption value \(w\).

outputFormat

Output the minimum value of \(|P-k|\), where \(P = \frac{S}{d}\) is the average soda consumption on the chosen path, \(S\) is the total soda amount, and \(d\) is the number of roads (days) in the trip. The answer should be printed with 6 decimal places.

sample

4 5
1 2 4
2 3 6
2 4 5
0.000000