#P1796. 汤姆斯的天堂梦
汤姆斯的天堂梦
Description
Thomas lives on a planet at level . The environment there is extremely harsh: hours of work every day and piles of garbage are unbearable. He longs for the heavenly life on a planet at level .
There are flights that take people from a lower-level planet to a planet one level higher. Sometimes you need to pay a certain amount to the pilot, and sometimes you can receive some money.
Thomas knows in advance all routes from the level planet to the level planet and the amount to pay (or receive) for each route. He wants to find a route with the lowest total price (or the highest total earnings).
Input Format
- The first line contains a positive integer (). The following data is divided into sections. In each section, the first line contains an integer (), indicating that there are planets at level .
- In the next lines, the -th line describes the planets at level that are connected to the planet at level with index , followed by the cost for each such flight (a positive number means paying, a negative number means earning; the absolute value does not exceed ). Each line consists of several “index cost” pairs and ends with a single . Each line contains at most routes.
Output Format
Output the required (or obtained) total cost. A positive number means paying, and a negative number means earning.
3
2
1 15 0
1 5 0
3
1 -5 2 10 0
1 3 0
2 40 0
2
1 1 2 5 3 -5 0
2 -19 3 -20 0
-1
Hint
For of the testdata, , .
Sample explanation:

Translated by ChatGPT 5
京公网安备 11011102002149号