uva 590 - Always on the run

1、http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=531

2、题目大意:

给定两个数n,k,n个城市,k次航班,然后给出n*(n-1)条航班记录,前(n-1)行代表第一个城市分别到其他城市的航班,如果消费为0,则代表没有航班,例如第一个样例,前两行表第一个城市到第二个城市和打三个城市的所有航班,接下来的(n-1)行,代表第二个城市到第一个城市和第三个城市的,现在要求出从第一个城市出发,经过k次航班,到达第n个城市所需要的最小花费是多少?

状态转移方程:dp[i][j]表示经过j次航班,到达城市i的最小花费

dp[i][j]初始为-1.dp[1][0]=0;

dp[i][j]=max(dp[i-1][j]+cost[i][j][c],dp[i][j]);

3、题目:



  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, \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 are d 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 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.



Miguel A. Revilla 
1998-03-10
4、AC代码;

#include<stdio.h>
#include<string.h>
#define N 15
#define K 1005
int day[N][K];
int cost[N][N][35];
int dp[N][K];
int n,k;
void DP()
{
    memset(dp,-1,sizeof(dp));
    int min;
   dp[1][0]=0;
    for(int j=1;j<=k;j++)
    {
        for(int i=1;i<=n;i++)
        {
            min=-1;
            for(int c=1;c<=n;c++)
            {
                if(i==c || dp[c][j-1]<0)
                continue;
                int t=(j-1)%day[c][i];
                if(cost[c][i][t]==0)
                continue;//没有航班
                if(min<0)
                min=dp[c][j-1]+cost[c][i][t];
                else if(min>dp[c][j-1]+cost[c][i][t])
                min=dp[c][j-1]+cost[c][i][t];
            }
            dp[i][j]=min;
        }
    }
    if(dp[n][k]<0)
    printf("No flight possible.\n\n");
    else
    printf("The best flight costs %d.\n\n",dp[n][k]);
}
int main()
{
    int cas=0;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        cas++;
        if(n==0 && k==0)
        break;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(i!=j)
                {scanf("%d",&day[i][j]);

                    for(int c=0;c<day[i][j];c++)
                    {
                        scanf("%d",&cost[i][j][c]);
                    }
                }
            }
        }
        printf("Scenario #%d\n",cas);
        DP();
    }
    return 0;
}


  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, \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 are d 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 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.



Miguel A. Revilla 
1998-03-10
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值