码迷,mamicode.com
首页 > 其他好文 > 详细

uva 590 Always on the run(dp)

时间:2015-04-01 11:29:34      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:acm   uva   c语言   dp   

uva 590 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 1,2,,n, where 1 is Paris, her starting point, and n is Atlanta, her final destination. The numbers will satisfy 2?n?10 and 1?k?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,,n), the next n - 1 lines to those from city 2 to all others ( 1,3,4,,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?d?30. Following this are d non-negative integers, representing the cost of the flight between the two cities on days 1,2,,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城市数,k航班数。接下来的n*(n-1)行代表的是城市i到城市j的航班情况1<=i,j<=n;i!=j,比如第一个样例的n*(n-1)行分别代表了:

第1个城市到第2个城市的航班情况

第1个城市到第3个城市的航班情况

第2个城市到第1个城市的航班情况

第2个城市到第3个城市的航班情况

第3个城市到第1个城市的航班情况

第3个城市到第2个城市的航班情况

航班情况的第一个数代表的是航班的周期数day,接下来的day个数是该航线在周期内不同时间的航班消费情况,求经过k个航班到达n号城市的最小花费。

解题思路:题意不好理解。i代表航班号,k代表城市号,now是当前周期,dp[i][k]记录的是在第i次航班到达k号城市的最小花费。

dp[i][k]=min(dp[i][k],dp[i?1][j]+rec[j][k].cost[now])

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;  
int n, m;
struct REC{
    int day, cost[35];
}rec[15][15];
int dp[1005][15];
int main() {
    int Case = 1;
    while (scanf("%d %d", &n, &m) == 2) {
        if (n == 0 && m == 0) break;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (i == j) continue;
                scanf("%d", &rec[i][j].day);
                for (int k = 1; k <= rec[i][j].day; k++) {
                    scanf("%d", &rec[i][j].cost[k]);
                }
            }
        }
        memset(dp, INF, sizeof(dp));
        dp[0][1] = 0;
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                for (int k = 1; k <= n; k++) {
                    if (j == k) continue;
                    int now = i % rec[j][k].day;    
                    if (!now) now = rec[j][k].day;
                    if (rec[j][k].cost[now]) {
                        dp[i][k] = min(dp[i][k], dp[i - 1][j] + rec[j][k].cost[now]);
                    }
                }
            }
        }
        printf("Scenario #%d\n", Case++);  
        if(dp[m][n] == INF) {  
            printf("No flight possible.\n");  
        }else {  
            printf("The best flight costs %d.\n", dp[m][n]);  
        }  
        printf("\n"); 
    }   
    return 0;
}

uva 590 Always on the run(dp)

标签:acm   uva   c语言   dp   

原文地址:http://blog.csdn.net/llx523113241/article/details/44802807

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!