Uva--590(动规)
2014-08-02 19:45:24
Always on the run |
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 , where 1 is Paris, her starting point, and n is Atlanta, her final destination. The numbers will satisfy and .
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 ared 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.
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 travelk 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.
思路: 题目很长。。。QAQ,代码不长。。。用dp[i][j]来记录乘了j次航班到达城市i的最小费用,那么只要从起点开始,递推下去,直到第k次航班。输出dp[终点][k]
1 /************************************************************************* 2 > File Name: m.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Thu 31 Jul 2014 10:15:52 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 const int INF = 1e9; 16 17 int dp[15][1005]; 18 int p[15][15][35]; 19 int day[15][15]; 20 int n,k; 21 22 int main(){ 23 int Case = 0; 24 while(scanf("%d%d",&n,&k) == 2 && (n || k)){ 25 for(int i = 0; i < n; ++i) 26 for(int j = 0; j < n; ++j){ 27 if(i == j) 28 continue; 29 scanf("%d",&day[i][j]); 30 for(int m = 0; m < day[i][j]; ++m){ 31 scanf("%d",&p[i][j][m]); 32 } 33 } 34 for(int i = 0; i < 15; ++i) 35 for(int j = 0; j < 1005; ++j) 36 dp[i][j] = INF; 37 dp[0][0] = 0; 38 for(int i = 0; i < k; ++i){ //天数 39 for(int j = 0; j < n; ++j){ //current postition 40 if(dp[j][i] == INF) 41 continue; 42 for(int m = 0; m < n; ++m){ //next position 43 if(j == m || p[j][m][i % day[j][m]] == 0) 44 continue; 45 dp[m][i + 1] = min(dp[m][i + 1],dp[j][i] + p[j][m][i % day[j][m]]); 46 //printf("dp[%d][%d] : %d\n",m,i + 1,dp[m][i + 1]); 47 } 48 } 49 } 50 printf("Scenario #%d\n",++Case); 51 if(dp[n - 1][k] >= INF) 52 printf("No flight possible.\n"); 53 else 54 printf("The best flight costs %d.\n",dp[n - 1][k]); 55 printf("\n"); 56 } 57 return 0; 58 }