Always on the run
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 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.
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个城市, k次航班,接下来输入n * (n - 1)行,表示第i个城市和其他城市的航班,0表示无航班,航班形成周期。要求出经过k次航班从城市1到城市n最少需要的钱。
思路:dp, dp[i][k]表示在城市i,经过k次航班的最小花费。j表示起点城市,i表示终点城市,k表示第k次航班。那么当次航班的价钱为v[j][i][(k - 1) % day[j][i] + 1]。
状态转移方程为dp[i][k] = min(dp[i][k], dp[j][k - 1] + v[j][i][(k - 1) % day[j][i] + 1]。
代码:
#include <stdio.h>
#include <string.h>
const int N = 15, INF = 2000000000;
int min(int a, int b) {
return a < b ? a : b;
}
int n, m;
int i, j, k;
int day[N][N], v[N][N][35];
int dp[N][1005];
int main(){
int tt = 1;
while (~scanf("%d%d", &n, &m) && n || m) {
for (i = 0; i <= n; i ++)
for (j = 0; j <= m; j ++)
dp[i][j] = INF;
dp[1][0] = 0;
for (i = 1; i <= n; i ++)
for (j = 1; j <= n; j ++) {
if (i == j) continue;
scanf("%d", &day[i][j]);
for (k = 1; k <= day[i][j]; k ++) {
scanf("%d", &v[i][j][k]);
}
}
for (k = 1; k <= m; k ++)
for (i = 1; i <= n; i ++)
for (j = 1; j <= n; j ++) {
if (i == j) continue;
int t = (k - 1) % day[j][i] + 1;
if (v[j][i][t] && dp[j][k - 1] != INF)
dp[i][k] = min(dp[i][k], dp[j][k - 1] + v[j][i][t]);
}
printf("Scenario #%d\n", tt ++);
if (dp[n][m] == INF)
printf("No flight possible.\n\n");
else
printf("The best flight costs %d.\n\n", dp[n][m]);
}
return 0;
}