uva 590 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 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*(n-1),每行分别表示第i个国家到第j个国家的航班。如样例1中,其中i到j的排列顺序是1->2, 1->3,2->1, 2->3, 3->1 ,3->2,每行先给出一个数d表示在第i个国家到第j个国家的花销。其中这d个数是循环的,比如3 1 2 3就是第1天的价格是1,第二天的价格是2,第三天的价格是3,第四天的价格是1,其中0表示当天没有航班。现在问你让你和警察周旋k后到达第n个地方所要花费的最小费用是多少?

代码如下:

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<string>
using namespace std;
const int maxint=999999;
int dp[1001][11];
vector<int> price[11][11];
void ini()
{
    memset(dp,0,sizeof(dp));
    for(int i=0;i<=10;i++)
    dp[0][i]=-1;
    for(int i=1;i<=1000;i++)
    for(int j=1;j<=10;j++)
    dp[i][j]=maxint;
    for(int i=0;i<=10;i++)
    for(int j=0;j<=10;j++)
    price[i][j].clear();
}
int getprice(int from,int to,int day)
{
    int len=price[from][to].size();
    return price[from][to][(day-1)%len];
}
int n,k,m;
int main()
{
    int tem,index=1;
    ios::sync_with_stdio(false);
    while(cin>>n>>k,n+k)
    {
        ini();
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(i!=j)
                {
                    cin>>m;
                    for(int q=1;q<=m;q++)
                    {
                        cin>>tem;
                        price[i][j].push_back(tem);
                    }
                }
            }
        }
        dp[0][1]=0;dp[1][1]=-1;
        for(int i=1;i<=k;i++)
        {
            for(int j=1;j<=n;j++)
            {
                tem=maxint;
                if(dp[i][j]==-1)
                continue;
                for(int q=1;q<=n;q++)
                {

                    if(q!=j&&dp[i-1][q]!=-1&&getprice(q,j,i)!=0)
                    tem=min(tem,dp[i-1][q]+getprice(q,j,i));

                }
                dp[i][j]=tem;
            }
        }
        cout<<"Scenario #"<<index++<<endl;
        if(dp[k][n]==maxint)
        cout<<"No flight possible."<<endl;
        else
        cout<<"The best flight costs "<<dp[k][n]<<"."<<endl;
        cout<<endl;
    }
    return 0;
}

思路:
简单的动态规划题目,转移方程并不难想。状态可以设置成3个也可以设置成2个,我设置成两个,其转移方程为dp[i][j]=min(dp[i][j],dp[i-1][q]+getprice(q,j,i)); 第一个状态时是天数,第二个状态是当前要去的位置,所以当前天数i要去的位置j=min(前一天所到达的位置q且该位置不是j+从q到j在第i天的票价)

注意:
每个结果输出两个换行,否则wa。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值