#P7232. [JSOI2014] 支线剧情 2
[JSOI2014] 支线剧情 2
Description
The otaku JYY really likes playing RPG games, and JYY always wants to spend the least time to watch all side plots. Recently, JYY bought a new genuine RPG game, so JYY can finally “load” and “save”!
In the RPG game JYY is playing now, there are plot points, numbered from to . At plot point , depending on JYY’s choices, JYY may go through different side plots and reach different new plot points. Of course, if is , it means that plot point is an ending of the game.
Watching a side plot takes some time. JYY starts at plot point , which is the beginning of the game. This game is special: from the beginning to any plot point, the side-plot path is unique. Also, every plot point is reachable from plot point .
Just like in the previous RPG game, JYY can choose to restart the game at any time, i.e. return to plot point . However, something is different this time: since JYY bought the genuine software, whenever JYY reaches a plot point, JYY can “save” and “load”. Unfortunately, this RPG game has only one save slot: each “save” operation overwrites the previous save. For example, if JYY performs “save” at plot point , then afterward, when JYY reaches any plot point (including an ending), JYY can “load” and immediately return to plot point . But if later JYY performs “save” at plot point , then when JYY “loads” again, JYY can only return to plot point .
At the beginning, the save slot stores no information, so JYY can only “load” after performing a “save” at least once. Also, “load”, “save”, and “restart” take no time.
Rewatching plots that have already been watched is very painful. JYY wants to spend the least time to watch all different side plots.
Input Format
The first line contains a positive integer .
The next lines describe the information of plot points. Line describes plot point .
In each line, the first non-negative integer is . Then follow pairs of integers and , meaning that from plot point one can go to plot point , and watching this side plot takes time.
Output Format
Output one integer in one line, the minimum time required for JYY to finish watching all side plots.
9
2 5 1 2 1
2 3 1 6 1
2 7 1 4 1
2 8 1 9 1
0
0
0
0
0
8
Hint
Sample Explanation 1
The best route is:
Go from to , then restart.
Go from to , save at , then go to , and load.
Go from to , save at , then go to , and load.
Go from to , save at , then go to , and load.
Finally go to . The total time is . It can be proven that there is no answer smaller than .
Constraints
For of the testdata, , , .
Translated by ChatGPT 5
京公网安备 11011102002149号