UVALive 4261——Trip Planning——————【dp+打印路径】

Trip Planning
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF
 


You are going on a long trip. Initially, you stay at hotel 0. Along the way, there are n<tex2html_verbatim_mark> hotels. The only place you are allowed to stop are at these hotels. The distance from hotel i - 1<tex2html_verbatim_mark> to hotel i<tex2html_verbatim_mark> is ai<tex2html_verbatim_mark> . You can choose which of the hotels you stop at. You must stop at the final hotel, which is your destination.

You would ideally like to travel 100 kilometers a day. However, this may not be possible. It depends on the spacing of the hotels. There is no limit on the distance you traveled in a day. If you travel x<tex2html_verbatim_mark> kilometers during a day, the penalty for that day is (x - 100)2<tex2html_verbatim_mark> . You want to plan your trip so as to minimize the total penalty -- that is, the sum, over all travel days, of the daily penalty. Write a program to determine the optimal sequence of hotels at which to stop.

Input

The input file contains a set of test data. Each test data consists of two parts. The first part is the number of hotels n<tex2html_verbatim_mark> . The second part is a sequence of n<tex2html_verbatim_mark> integers a1a2,..., an<tex2html_verbatim_mark> . Each ai<tex2html_verbatim_mark> is the distance between hotel i - 1<tex2html_verbatim_mark> and hotel i<tex2html_verbatim_mark> . Assume that 0 < ai < 200<tex2html_verbatim_mark> . They may be written in many lines. Assume that n < 1000<tex2html_verbatim_mark> , and n = 0<tex2html_verbatim_mark> signals the end of the test data.

Output

The first line of the output is the minimum penalty p<tex2html_verbatim_mark> . The second line of the output is the indexes of the hotels to stop at. If the solution is not unique, print the one with fewer stops. If there are more then one solutions with the same number of stops, print the one which is the lexicographically smallest one. For example (1 2 4) < (1 3 4)<tex2html_verbatim_mark> . Print 30 stops in each line, except the last line which may contain less stops. Print a blank line between datasets.

Sample Input

10 
11 48 28 87 35 86 37 83 16 34 
20 
81 49 50 87 107 20 40 84 60 47 29 30 35 47 108 41 85 106 77 106 
0

Sample Output

p=2271 
 0 3 5 7 10 

p=4617 
 0 1 3 4 6 8 11 14 15 17 18 19 20


题目大意:给出n,表示n座城市,ai表示城市i-1与城市i的距离。然后一个惩罚值 (x - 100)2表示一天中行驶x距离时会受到的惩罚值。问你到达城市n时受到最少的惩罚值是多少,以及行驶的路径。

解题思路:定义dp[i]表示到达城市i所受惩罚的最小值。dq[i][j]表示一天内从i城市到达城市j的所受惩罚值。sum[i]表示从0城市到达城市i的距离和。dp[j] = min(dp[j], dp[i]+dq[i][j])。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int maxn = 1e4+200;
const int INF = 0x3f3f3f3f;
typedef long long INT;
INT dp[maxn] , dq[maxn][maxn];
int a[maxn] ,sum[maxn] ,path[maxn];
int ans[maxn];
int main(){
    int n; int cnt = 0;
    while(scanf("%d",&n)!=EOF&&n){
        cnt++;
        if(cnt != 1)
            puts("");
        for(int i = 1; i <= n; i++){
            scanf("%d",&a[i]);
            sum[i] = sum[i-1] + a[i];
        }
        for(int i = 0; i <= n; i++){
            for(int j = i + 1; j <= n; j++){
                INT tmp = (sum[j] - sum[i] - 100);
                dq[i][j] = tmp * tmp;
                dq[j][i] = dq[i][j];
            }
        }
        for(int i = 0; i<=n+10;i++){
            dp[i] = INF;
        }
        dp[0] = 0;
        for(int i = 0; i < n; i++){
            for(int j = i+1; j <= n; j++){

                if(dp[j] > dp[i] + dq[i][j]){
                    dp[j] = dp[i] + dq[i][j];
               //     printf("%d %d++++
",i,j);
                    path[j]=i;      //记录路径
                }
            }
            //printf("%d++++
",dp[n]);
        }
        printf("p=%lld
",dp[n]);
        int pos=n; int coun = 1;
        while( pos ){
            ans[coun++] = pos;
            pos=path[pos];
        }
        int cn = 1;
        printf(" 0");
        for(int i = coun-1; i >= 1; i--){
            cn++;
            printf(" %d",ans[i]);
            if(cn%30 == 0){
                puts("");
            }
        }puts("");
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/chengsheng/p/4887045.html