Vacation Plans
Time limit1sMemory limit512 MB
Find the minimum total cost for up to three people to each travel from city 1 to their airports across separate countries in the same number of days.
- Level
Hard8 of 10
- Topics
- Shortest path, Dynamic programming, Graph, Math
- Solved
- No attempts yet
Problem
A group of people plans a winter vacation on a remote island near the equator. Every member lives in a different country, and the island is reachable only by airplane. Each country has exactly one airport, so every member has to reach the airport city of their own country. The group agreed that everyone is at their airport on the same day, and that day does not have to be the first day of the trip.
Each country has cities and one way roads. City 1 is the home city, and city holds the airport. On day 0 every member is in city 1 of their own country. On each day after that, a member either travels along one road out of the current city, or stays in the current city. Travelling from city to city along a road costs , and staying in city costs the cheapest hotel price of that city. Every member picks exactly one of the two options every day.
The members pool their money, so they plan as a group. After the same number of days , every member has to be in the airport city of their own country. can be 0. Find the smallest possible total cost.
Roads are directed, and the cost from to does not have to equal the cost from to . No road connects a city to itself. In every country at least one route runs from city 1 to the airport city.

Figure L.1. The city layouts of two members. The airports are in city 4 and city 3, and the home city is 1 for both. is the cost of travelling along a road, and is the cheapest hotel price.
In Figure L.1 the cheapest plan works like this. On day 1 the first member moves to city 3 and the second member moves to city 2. On day 2 the first member moves to city 4 and the second member moves back to city 1. On day 3 the first member stays in city 4 and the second member moves to city 3. The cost is .
Input
The first line contains the integer , the number of members in the group and therefore also the number of countries ().
The descriptions of the countries follow. The first line of a country description contains two integers and , the number of cities and the number of roads (, ). The next lines contain one integer each, the cheapest hotel price of city 1 through city in that order (). The next lines each contain three integers , and , meaning that a one way road runs from city to city at cost (, , ). The last line of a country description contains the integer , the city that holds the airport (). City 1 is the home city in every country.
Output
Print one line with a single integer, the smallest total cost that puts every member in the airport city of their own country after the same number of days.