This page is still under construction.

Parts of this page are still being built. What you see may change.

Always on the run

Time limit1sMemory limit128 MB

Summary
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 nn and kk. Here nn is the number of cities through which Trisha's escape may take her, and kk is the number of flights she will take. The cities are numbered 1,2,…,n1, 2, \ldots, n, where city 11 is her starting point and city nn is her final destination. The values satisfy 2≤n≤102 \le n \le 10 and 1≤k≤10001 \le k \le 1000.

Next come n(n−1)n(n-1) flight schedules, one per line, describing the connection for every ordered pair of distinct cities. The first n−1n-1 schedules are the flights from city 11 to all other cities (2,3,…,n2, 3, \ldots, n), the next n−1n-1 lines are the flights from city 22 to all others (1,3,4,…,n1, 3, 4, \ldots, n), and so on.

Each flight schedule starts with an integer dd, the length of its period in days, with 1≤d≤301 \le d \le 30. It is followed by dd non-negative integers giving the cost of the flight on days 1,2,…,d1, 2, \ldots, d. A cost of 00 means there is no flight on that day.

For example, the schedule 3 75 0 80 means the flight costs 7575 on day 11, is unavailable on day 22, costs 8080 on day 33, and then the cycle repeats: it costs 7575 on day 44, is unavailable on day 55, and so on.

The input ends with a scenario in which n=k=0n = k = 0; this scenario must not be processed.

Output

For each scenario, first print its number in the form Scenario #i, where ii counts the scenarios starting from 11.

Trisha starts in city 11. On each of the kk days she flies to a city different from the one she is currently in. If it is possible for her to arrive in city nn after exactly kk flights, print The best flight costs x., where xx is the least total cost of the kk flights. Otherwise, print No flight possible.

Separate consecutive scenarios with a single blank line.

Examples1

  1. Example 1

    Input
    3 6
    2 130 150
    3 75 0 80
    7 120 110 0 100 110 120 0
    4 60 70 60 50
    3 0 135 140
    2 70 80
    2 3
    2 0 70
    1 80
    0 0
    
    Expected output
    Scenario #1
    The best flight costs 460.
    
    Scenario #2
    No flight possible.