UVA 590 Always on the run

来源:互联网 发布:识别植物的软件 编辑:程序博客网 时间:2024/05/16 01:05

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 kn 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 ared 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 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 62 130 1503 75 0 807 120 110 0 100 110 120 04 60 70 60 503 0 135 1402 70 802 32 0 701 800 0

Sample Output 

Scenario #1The best flight costs 460.Scenario #2No flight possible.
给出n个城市和之间的航班情况,问第k天到n城市的最少花费,价格为0代表没有航班,而航班是会循环的。
#include<algorithm>#include<cstdio>#include<cstring>#include<iostream>#include<cmath>using namespace std;const int maxn = 1005;int a[11][11][maxn], f[11][maxn];int n, t, i, j, k;int main(){int tot = 0;while (cin >> n >> t, n, t){memset(a, 0, sizeof(a));memset(f, 1, sizeof(f));for (i = 1; i <= n;i++)for (j = 1; j <= n;j++)if (i != j){cin >> a[i][j][0];for (k = 1; k <= a[i][j][0]; k++) cin >> a[i][j][k];}f[1][0] = 0;for (k = 1; k <= t;k++)for (i = 1; i <= n; i++)for (j = 1; j <= n; j++)if (i != j){int u = (k - 1) % a[j][i][0] + 1;if (a[j][i][u]) f[i][k] = min(f[i][k], f[j][k - 1] + a[j][i][u]);}if (f[n][t] - f[0][0] >= 0) printf("Scenario #%d\nNo flight possible.\n\n", ++tot);else printf("Scenario #%d\nThe best flight costs %d.\n\n", ++tot, f[n][t]);}}


0 0