Algorithm title: UVA 590 Always on the run (DP)

Source: Internet
Author: User

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 UN Expected. So we are, the wrapped frame tucked firmly under her arm, running to catch the northbound Metro to Charles-de-gaulle Airport.

But even more important than actually stealing the painting was to shake off the police that would soon be following. Trisha ' s plan are simple:for several days she'll be flying from one city to another, making one flight each day. When she's reasonably sure that police has lost her trail, she'll fly to Atlanta and meet her ' customer ' (known onl Y as Mr. P.) to deliver the painting.

Her plan are complicated by the fact this nowadays, even when you are stealing expensive art, and have to watch your NG budget. Trisha therefore wants to spend the least money possible on her escape flights. This isn't easy, since airlines prices and flight availability vary from day to day. The price and availability of a airline connection depends on the two cities and involved. Every pair of cities has a ' flight schedule ' which repeats-Every few days. The length of the period may is different for each pair of cities and for each direction.

Although Trisha is a good in stealing paintings, she easily gets confused when booking airline. This is where your come in.

Input

The input file contains the descriptions of several scenarios in which Trisha to escape. Every description starts with a line containing two integers n and K. N are the number of cities through which Trisha ' s ESC Ape may take her, and K are the number of flights she and she take. The cities are numbered and

Next to are given n ( n -1) flight schedules, one per line, describing the connection between P Ossible pair of cities. The ' 1 flight schedules correspond to the ' flights from ' City 1 to all ' other cities ( algorithm title: UVA 590 Always on the run (DP)-b340 590DP ", the next n -1 lines to those from City 2 To all others (), and so on .

The description of the flight schedule itself starts with a integer d, the length of the period in, with . Following this are D non-negative integers representing the cost of the flight between the two 3>. A cost of 0 means this there is no flight between the two of

So, for example, the flight schedule '3 0' means ' and ' on the ' the ' flight costs Day there are no flight, on the third day it costs, and then the cycle Repeats:on the fourth day Flight 75, There is no flight on the fifth day, etc.

The input is terminated by a scenario has n = k = 0.

Output

For each scenario in the input, the scenario of the the number of the, as shown in the sample output. If It is possible for Trisha to travel K days, starting in City 1, each day flying to a different city than the day before , and finally (after K days) arriving in city N, then print ' The bestflight costs X. ', where x is the least AM Ount That's K flights can cost.

If It isn't possible to travel in such a way, print 'No flight possible.

Print a blank line after each scenario.

Sample Input

3 6 2 130, 3 0 7, 0 0, 4 3, 0,
135 140
2
, 2 3 2 0
1
0 0

Sample Output

Scenario #1 the best
flight costs 460.

Scenario #2
No flight possible.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.