#P2402. 奶牛隐藏
奶牛隐藏
Description
There are fields on a farm. One afternoon, a herd of cows is grazing in the fields. They are spread across many fields of the farm, which are connected by undirected roads, each with a different length.
Suddenly, heavy rain falls. The cows are very confused and want to find shelter quickly. It is known that each field has a barn, but each barn can only hold a limited number of cows. If the number of cows exceeds this capacity, the extra cows must go to other fields to shelter. Moving a distance of costs time . The cows want to know the minimum time needed for all of them to get into barns, i.e., the minimum time the last cow needs to get into a barn.
Input Format
- The first line contains two integers, the number of fields and the number of roads .
- The next lines each contain two integers. On the -th line, the integers denote the number of cows in field and the maximum capacity of the barn at that field, respectively.
- The next lines each contain three integers , indicating there is an undirected road of length connecting and .
Output Format
Output a single integer, the minimum time required for all cows to get into barns. If it is impossible for all cows to find shelter, output .
3 4
7 2
0 4
2 6
1 2 40
3 2 70
2 3 90
1 3 120
110
Hint
Sample Input/Output 1 Explanation:
The two cows at node go directly into barn . Among the remaining cows, run to node , and one goes along to get into barn . The cows at node also go directly into its barn. In this arrangement, the slowest cow spends time .
Constraints:
For of the data, it is guaranteed that:
- , .
- , , .
Translated by ChatGPT 5
京公网安备 11011102002149号