程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Uva 590 Always on the run (Dp)

Uva 590 Always on the run (Dp)

編輯:C++入門知識

Uva 590 Always on the run (Dp)


590 - Always on the run

Time limit: 3.000 seconds

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 Charles-de-Gaulle airport.

But even more important than actually stealing the painting is to shake off the police that will soon be following her. Trisha's plan is simple: for several days she will be flying from one city to another, making one flight per day. When she is reasonably sure that the police has 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 the least money possible on her escape flights. This is not easy, since airlines prices and flight availability vary from day to day. The price and availability of an airline connection depends on the two cities involved and the day of travel. Every pair of cities has a `flight schedule' which repeats every few days. The length of the period may be different for each pair of cities and for each direction.

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

Input

The input file contains the descriptions of several scenarios in which Trisha tries to escape. Every description starts with a line containing two integers n and k. n is the number of cities through which Trisha's escape may take her, and k is the number of flights she will take. The cities are numbered$1, 2, \dots, n$, where 1 is Paris, her starting point, and n is Atlanta, her final destination. The numbers will satisfy $2 \le? n \le? 10$ and $1 \le? k \le? 1000$.

Next you are given n(n - 1) flight schedules, one per line, describing the connection between every possible pair of cities. The first n - 1 flight schedules correspond to the flights from city 1 to all other cities ($2, 3, \dots, n$), the next n - 1 lines to those from city 2 to all others ( $1, 3, 4, \dots, n$), and so on.

The description of the flight schedule itself starts with an integer d, the length of the period in days, with $1 \le? d \le? 30$. Following this are d non-negative integers, representing the cost of the flight between the two cities on days $1, 2, \dots, d$. A cost of 0 means that there is no flight between the two cities on that day.

So, for example, the flight schedule ``3 75 0 80'' means that on the first day the flight costs 75, on the second day there is no flight, on the third day it costs 80, and then the cycle repeats: on the fourth day the flight costs 75, there is no flight on the fifth day, etc.

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

Output

For each scenario in the input, first output the number of the scenario, 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 best flight costs x.'', where x is the least amount that the k flights can cost.

If it is not possible to travel in such a way, print ``No flight possible.''.

Print a blank line after each scenario.

Sample 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

Sample Output

Scenario #1
The best flight costs 460.

Scenario #2
No flight possible.


題意 : 一個人要在n個城市(城市編號1,2,3,...,n)之間游玩k天,他的起始

城市是 城市1,目標城市是城市 n。

給你 n , k 的值 。 然後有 n*(n-1) 行 , 第 i 個(n-1 ) 行表示城市 i 到其它

城市的飛機的時間點的價格。

例如 3 75 0 80 表示 第一天價格是 75 ,第二天沒有航班,第三天價格是 80

第四天價格是 75 ,第五天沒有航班,第六天價格是 80

......

題目問你那個游玩的人能否在第 k 天到達 城市 n ,如果能,輸出最小的花

費 ,否則輸出 “ No flight possible.”


思路 : dp[ i ][ j ] 表示第 i 天到達城市 j 的最小的花費 。dp[ i ][ j ] 初始化為 -1 。

即當 j 城市在第 i 天能到達 城市k 的話 (假設花費為cost,前提dp[ i-1 ][ j ] != -1) :

if(dp[ i ] [ k ] == -1 ) dp[ i ][ k ]=dp[ i-1 ][ j ]+cost;
else dp[ i ][ k ]=min(dp[ i ][ k ],dp[ i-1 ][ j ]+cost);




#include 
#include 
#include 
#include 
using namespace std;
const int N=1010;
const int M=15;

int dp[N][M],n,m,cnt;
vector  a[M][M];

void initial()
{
    memset(dp,-1,sizeof(dp));
    for(int i=0; i

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved