Restaurant Recommendations
Time limit3sMemory limit128 MB
Restaurants recommend each other through a favorite digraph; find the cheapest cost to visit exactly k restaurants for every k, where each step's price depends on whether the recommender is a favorite of the current restaurant.
- Level
Hard8 of 10
- Topics
- Graph, Dynamic programming, Shortest path, Greedy
- Solved
- No attempts yet
Problem
Luka's town has restaurants, numbered from 1 to , and everyone can find something to like there. The restaurant owners also have favorite restaurants that they like to visit. If you ask a restaurant owner for a recommendation, the owner recommends their own restaurant and their favorite restaurants, and also every restaurant that the owners of those favorites would recommend.
In other words, the owner of restaurant recommends restaurant itself and every restaurant that can be reached from by following favorites one or more times.
The table below shows an example with four restaurants.
Luka plans to visit several restaurants as follows:
- He chooses the first restaurant freely.
- To choose each next restaurant, he asks the owner of the current restaurant for a recommendation and picks one of the recommended restaurants that he has not visited yet.
- Luka can end the tour at any moment.
Each restaurant has two prices for its main menu, and . When Luka enters a restaurant, its owner asks him who recommended the restaurant. If that person is the owner of restaurant , Luka pays:
- kuna if the owner of restaurant recommends restaurant ,
- kuna otherwise. Luka also pays this amount at the first restaurant.
Let be the largest number of restaurants Luka can visit this way. For every from to , find the minimum number of kuna Luka needs to visit exactly restaurants.
Input
The first line contains an integer (), the number of restaurants.
Each of the next lines contains several integers.
The first two numbers on the -th line are the main menu prices and (). The third number () is the number of favorite restaurants of the owner of restaurant . The remaining numbers are the labels of those favorite restaurants. The labels are pairwise distinct, and none of them equals .
Output
If is the largest number of restaurants Luka can visit, print lines. On the -th line, print the minimum number of kuna Luka must pay to visit exactly restaurants.
Hint
In the first example, the cheapest way to visit one restaurant is to visit restaurant 1 (200 kuna).
The cheapest way to visit two restaurants is to visit restaurant 3 (250 kuna) and then restaurant 2 (200 kuna).
The cheapest way to visit three restaurants is to visit restaurant 1 (200 kuna), restaurant 3 (250 kuna), and finally restaurant 2 (200 kuna).
The cheapest way to visit four restaurants is to visit restaurant 1 (200 kuna), restaurant 3 (250 kuna), restaurant 2 (200 kuna), and finally restaurant 4 (300 kuna).