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.
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 (), 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 an integer d, the length of the period in days, with . Following this are d non-negative integers, representing the cost of the flight between the two cities on days . 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.
If it is not possible to travel in such a way, print ``No flight possible.''.
Print a blank line after each scenario.
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
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