#P6310. 「Wdsr-1」仓库建设
「Wdsr-1」仓库建设
Background
One day, a famine broke out in the Human Village.
Problem Description
Because Gensokyo promotes the idea of a “community with a shared future of Gensokyo”, the kappa decided to help humans build some granaries to prevent famine from happening again.
The Human Village has cities and bidirectional roads. The -th road connects cities and , with length units.
Since each city has a different technology level, grain-transport trucks starting from different cities have different fuel capacities. The grain-transport trucks in the -th city can only support driving for units. Of course, cities are friendly to each other: no matter which city the truck arrives at, the enthusiastic local people will refuel it to full.
Kappa technology is highly advanced, so the amount of grain a warehouse can store can be considered infinite. Only warehouses can dispatch grain-transport trucks.
Now choose some cities to build warehouses so that every city can receive grain from a warehouse. To save resources, the kappa want the number of warehouses to be as small as possible.
However, youkai may sometimes occupy a city, so you need to compute the minimum number of warehouses needed when an arbitrary city is not allowed to build a warehouse.
Input Format
The first line contains two integers , with meanings as described above.
The next lines each contain 3 integers , with meanings as described above.
The next line contains integers , with meanings as described above.
Output Format
Output one line with integers.
The first integer indicates the minimum number of warehouses needed. The next integers: the -th integer indicates the minimum number of warehouses needed when city is not allowed to build a warehouse.
If, when city is not allowed to build a warehouse, building warehouses in other cities still cannot reach this city, output -1.
4 4
1 2 2
1 3 3
2 4 1
3 4 4
2 1 3 2
1 1 1 -1 1
Hint
Sample Explanation

This is the graph drawn for the sample. Obviously, building a warehouse in city 3 solves the problem. When city 3 is not allowed to build a warehouse, no matter how you build warehouses in other cities, grain still cannot be transported to city 3, so output -1.
Constraints
For of the testdata, is guaranteed.
For another of the testdata, is guaranteed.
For of the testdata, , , are guaranteed, and the graph is connected.
Translated by ChatGPT 5