Restaurant recommendation
Time limit3sMemory limit1024 MB
On a tree of cities, choose restaurants whose delivery balls do not overlap, maximizing the total preference of the chosen set.
- Level
Medium7 of 10
- Topics
- Tree, Dynamic programming, Divide and conquer
- Solved
- No attempts yet
문제
The KOI country has cities, numbered 1 through .
The road network of KOI country is a tree when cities are vertices and roads are bidirectional edges. A tree is a connected graph with no cycles.
There are restaurants in total, numbered 1 through . Some cities may have no restaurant, and some cities may have two or more.
Restaurant () is in city , has delivery range , and has customer preference . Restaurant can deliver only to cities reachable from city within roads or fewer. Let be the set of cities restaurant can deliver to, where is the length of the shortest path between cities and , that is, the minimum number of roads to cross between them.
You operate a delivery app. To avoid overlapping service, you want to choose a set of restaurants from the restaurants such that no city belongs to the delivery sets of two or more restaurants in . That is, for any two different restaurants and in , .
Among all valid sets , find the one with the largest total preference and print that total.
입력
The first line contains two integers and , separated by a space.
Each of the next lines contains two integers and , separated by a space, meaning there is a road between cities and .
Each of the next lines contains three integers , , and , separated by spaces, giving the city, delivery range, and preference of restaurant ().
출력
Print on the first line the largest total preference of a valid set .
제한
- All input values are integers.
- For every (): ,