Always on the run
Time limit1sMemory limit128 MB
Find the cheapest sequence of exactly k daily flights from city 1 to city n, where each ordered pair has a periodic price schedule over days.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Graph, Shortest path, Implementation
- Solved
- No attempts yet
Problem
Screeching tires. Searching lights. Wailing sirens. Police cars everywhere. Trisha Quickfinger did it again! Stealing the 'Mona Lisa' had been more difficult than planned, but being the world's best art thief means expecting the unexpected. So here she is, the wrapped frame tucked firmly under her arm, running to catch the northbound metro to the airport.
But even more important than actually stealing the painting is shaking off the police who will soon be following her. Trisha's plan is simple: for several days she will fly from one city to another, taking exactly one flight per day. When she is reasonably sure the police have lost her trail, she will fly to Atlanta and meet her 'customer' (known only as Mr. P.) to deliver the painting.
Her plan is complicated by the fact that nowadays, even when you are stealing expensive art, you have to watch your spending budget. Trisha therefore wants to spend as little money as possible on her escape flights. This is not easy, since airline prices and flight availability vary from day to day. The price and availability of a connection depend on the two cities involved and on the day of travel. Every ordered pair of cities has a flight schedule that repeats every few days; the length of the period may differ for each pair of cities and for each direction.
Although Trisha is good at stealing paintings, she easily gets confused when booking flights. This is where you come in.
Input
The input contains the descriptions of several scenarios.
Every scenario starts with a line containing two integers and . Here is the number of cities through which Trisha's escape may take her, and is the number of flights she will take. The cities are numbered , where city is her starting point and city is her final destination. The values satisfy and .
Next come flight schedules, one per line, describing the connection for every ordered pair of distinct cities. The first schedules are the flights from city to all other cities (), the next lines are the flights from city to all others (), and so on.
Each flight schedule starts with an integer , the length of its period in days, with . It is followed by non-negative integers giving the cost of the flight on days . A cost of means there is no flight on that day.
For example, the schedule 3 75 0 80 means the flight costs on day , is unavailable on day , costs on day , and then the cycle repeats: it costs on day , is unavailable on day , and so on.
The input ends with a scenario in which ; this scenario must not be processed.
Output
For each scenario, first print its number in the form Scenario #i, where counts the scenarios starting from .
Trisha starts in city . On each of the days she flies to a city different from the one she is currently in. If it is possible for her to arrive in city after exactly flights, print The best flight costs x., where is the least total cost of the flights. Otherwise, print No flight possible.
Separate consecutive scenarios with a single blank line.