Multi Path Story
Time limit5sMemory limit512 MB
Given a branching-point DAG where every run restarts at point 1, find the minimum total time to traverse every choice at least once.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path
- Solved
- No attempts yet
Problem
You are a programmer who loves pretty girl games, a sub-genre of dating simulation games. "Floatable Heart", released last Friday, has just arrived at your home. The game has several stories. When you complete all of them, you get a special figure of the main heroine, Megumi. You want to start playing right away, but first you need to work out how to complete all the stories in the shortest time.
You have a special skill that reveals the branching structure of a game. Using it, you found that the game has branching points, and the -th branching point has choices. The game is complicated, so several routes can reach the same -th branching point. Selecting the -th choice at the -th branching point takes minutes to reach another branching point (or an ending).
To complete all the stories, you must select every choice at every branching point and read the story between each branching point and the next one (or an ending). You can assume that a reset back to the beginning, and the trip from the beginning to the first branching point, take negligible time.
The manual says the game has a "Quick Save" feature, which records your current position so you can return to it later. A bug keeps this feature from working. So whenever you reach an ending or quit the game partway, you must restart from the first branching point. No patch for the bug has been released. This is a hard constraint even for the fastest players.
Find the shortest total time needed to complete all the stories.
Input
The input is given in the following format.
n
k1 t11 c11 ... t1k1 c1k1
:
kn tn1 cn1 ... tnkn cnkn
The first line contains the number of branching points (). The next lines describe the branching points with IDs to . The first integer () is the number of choices at branching point . If , branching point is an ending. Then come integers: () is the ID of the branching point you reach by selecting choice , and () is the time to read the story between branching point and .
The branching point with ID 1 is the first branching point. Every branching point and ending is reachable from the first branching point. The game has no loops: without a reset, no branching point is reached more than once.
Output
Print the shortest time needed to complete all the stories on a single line.