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.
Hard8GraphDynamic programmingShortest pathGreedyNo attempts yetTime limit3sMemory limit128 MBLuka's town has N restaurants, numbered from 1 to N, 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 i recommends restaurant i itself and every restaurant that can be reached from i by following favorites one or more times.
The table below shows an example with four restaurants.
| Restaurant owner | Favorite restaurants | Recommended restaurants |
|---|---|---|
| 1 | 2 | 1, 2, 3, 4 |
| 2 | 3 | 2, 3, 4 |
| 3 | 2, 4 | 2, 3, 4 |
| 4 | none | 4 |
Luka plans to visit several restaurants as follows:
Each restaurant A has two prices for its main menu, XA and YA. When Luka enters a restaurant, its owner asks him who recommended the restaurant. If that person is the owner of restaurant B, Luka pays:
Let K be the largest number of restaurants Luka can visit this way. For every k from 1 to K, find the minimum number of kuna Luka needs to visit exactly k restaurants.
The first line contains an integer N (1≤N≤1000), the number of restaurants.
Each of the next N lines contains several integers.
The first two numbers on the i-th line are the main menu prices Xi and Yi (1≤Xi,Yi≤10000). The third number Oi (0≤Oi<N) is the number of favorite restaurants of the owner of restaurant i. The remaining Oi numbers are the labels of those favorite restaurants. The labels are pairwise distinct, and none of them equals i.
If K is the largest number of restaurants Luka can visit, print K lines. On the k-th line, print the minimum number of kuna Luka must pay to visit exactly k restaurants.
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).