首页 > ACM题库 > HDU-杭电 > Hdu 1474 Always On the Run-动态规划[解题报告] C++
2013
12-11

Hdu 1474 Always On the Run-动态规划[解题报告] C++

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.

输入:

The input 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.

输出:

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.

样例输入:

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

样例输出:

Scenario #1
The best flight costs 460.

Scenario #2
No flight possible.


题意:

有n座城市,Trisha要求在这n座城市旅游k天,从城市1出发,第k天到达城市n。

输入有n*(n-1)行,每n-1行代表i到除了i之外的其他城市航班的天数以及价格。

求出Trisha的最小花费。

思路:

链式dp,dp[i][d]代表第d天到达i城市所需要的最小代价,于是dp[i][d] = min(dp[i][d], dp[j][d-1] + price[j][i][X])。

意思是:第d天到达i城市所花费的代价是,第d-1天到达j城市 + j到i的价格 最小的一个。

由于初识状态只有一个,即从城市1出发。所以初始值只有一个dp[1][0] = 0。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>

#define min(a,b) (((a) < (b)) ? (a) : (b))

int day[15][15];
int price[15][15][35];
int dp[15][1005];

int main()
{
    int n, k;
    int cases = 0;
    while (scanf("%d %d", &n, &k) && n && k)
    {
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= n; ++j)
                if (i != j) 
                {
                    scanf("%d", &day[i][j]);
                    for (int d = 1; d <= day[i][j]; ++d)
                        scanf("%d", &price[i][j][d]);
                }

        for (int i = 0; i <= n; ++i)
        {
            for (int d = 0; d <= k; ++d)
                dp[i][d] = INT_MAX;
        }
        dp[1][0] = 0;

        for (int d = 1; d <= k; ++d)
            for (int i = 1; i <= n; ++i)
                for (int j = 1; j <= n; ++j)
                    if (i != j)
                    {
                        int q = (d - 1) % day[j][i] + 1;
                        if (price[j][i][q] && dp[j][d-1] != INT_MAX)
                            dp[i][d] = min(dp[i][d], dp[j][d-1] + price[j][i][q]);
                    }

        printf("Scenario #%d\n", ++cases);
        if (dp[n][k] != INT_MAX)
            printf("The best flight costs %d.\n\n", dp[n][k]);
        else
            printf("No flight possible.\n\n");
    }
    return 0;
}

转自:http://www.cnblogs.com/kedebug/archive/2012/11/19/2776892.html


  1. #include <cstdio>
    #include <cstring>

    const int MAXSIZE=256;
    //char store[MAXSIZE];
    char str1[MAXSIZE];
    /*
    void init(char *store) {
    int i;
    store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
    for(i=’F';i<=’Z';++i) store =i-5;
    }
    */
    int main() {
    //freopen("input.txt","r",stdin);
    //init(store);
    char *p;
    while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
    if(p=fgets(str1,MAXSIZE,stdin)) {
    for(;*p;++p) {
    //*p=store[*p]
    if(*p<’A’ || *p>’Z') continue;
    if(*p>’E') *p=*p-5;
    else *p=*p+21;
    }
    printf("%s",str1);
    }
    fgets(str1,MAXSIZE,stdin);
    }
    return 0;
    }

  2. 第一句可以忽略不计了吧。从第二句开始分析,说明这个花色下的所有牌都会在其它里面出现,那么还剩下♠️和♦️。第三句,可以排除2和7,因为在两种花色里有。现在是第四句,因为♠️还剩下多个,只有是♦️B才能知道答案。

  3. if(j){
    int ans=a ;
    for(int x=j-1;x>=0;x–){
    if(!a ) break;
    ans=min(ans,a );
    sum+=ans;
    }
    }
    求解释,,dp的思路是什么呢?

  4. 思路二可以用一个长度为k的队列来实现,入队后判断下队尾元素的next指针是否为空,若为空,则出队指针即为所求。