Trip Planning

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

Description

 

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++++\n",i,j);
path[j]=i; //记录路径
}
}
//printf("%d++++\n",dp[n]);
}
printf("p=%lld\n",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;
}

  

UVALive 4261——Trip Planning——————【dp+打印路径】的更多相关文章

  1. 【noi 2.6_2000】&【poj 2127】 最长公共子上升序列 (DP+打印路径)

    由于noi OJ上没有Special Judge,所以我是没有在这上面AC的.但是在POJ上A了. 题意如标题. 解法:f[i][j]表示a串前i个和b串前j个且包含b[j]的最长公共上升子序列长度 ...

  2. UVa11404 - Palindromic Subsequence(区间DP+打印路径)

    题目大意 给定一个字符串,要求你删除尽量少的字符,使得原字符串变为最长回文串,并把回文串输出,如果答案有多种,则输出字典序最小的 题解 有两种解法,第一种是把字符串逆序,然后求两个字符串的LCS,并记 ...

  3. John's trip(POJ1041+欧拉回路+打印路径)

    题目链接:http://poj.org/problem?id=1041 题目: 题意:给你n条街道,m个路口,每次输入以0 0结束,给你的u v t分别表示路口u和v由t这条街道连接,要输出从起点出发 ...

  4. POJ 1141 Brackets Sequence(区间DP, DP打印路径)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  5. UVALive 4262——Trip Planning——————【Tarjan 求强连通分量个数】

    Road Networks Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Stat ...

  6. UVA 531 - Compromise(dp + LCS打印路径)

      Compromise  In a few months the European Currency Union will become a reality. However, to join th ...

  7. FatMouse's Speed ~(基础DP)打印路径的上升子序列

    FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take ...

  8. PACM Team(牛客第三场多校赛+dp+卡内存+打印路径)

    题目链接(貌似未报名的不能进去):https://www.nowcoder.com/acm/contest/141/A 题目: 题意:背包题意,并打印路径. 思路:正常背包思路,不过五维的dp很容易爆 ...

  9. UVA 1626 区间dp、打印路径

    uva 紫书例题,这个区间dp最容易错的应该是(S)这种匹配情况,如果不是题目中给了提示我就忽略了,只想着左右分割忘记了这种特殊的例子. dp[i][j]=MIN{dp[i+1][j-1] | if( ...

随机推荐

  1. grep 不打印本身

    显示java进程的同时还会把“grep java”这个进程打印出来“root 5523 5203 0 10:49 pts/0 00:00:00 grep java”,为了不打印此行,有以下几种方式,大 ...

  2. HTTP返回码中301与302的区别

    一.官方说法 301,302 都是HTTP状态的编码,都代表着某个URL发生了转移,不同之处在于: 301 redirect: 301 代表永久性转移(Permanently Moved). 302 ...

  3. shell脚本备份系统的方法

    linux自动备份shell(使用全备份,增量备份策略) 在cron里设置,每周日晚12点执行(每周日全备份,其余时间增量备份)#vi backup.sh #!/bin/bash # definewe ...

  4. mongodb 分页(limit)

    db.COLLECTION_NAME.find().limit(NUMBER) db.mycol.find().limit() db.mycol.find({},{,_id:}).limit().sk ...

  5. Express的日志模块morgan

    morgan 是nodejs的一个日志模块,由 express 团队维护. 这里通过示例简要介绍morgan模块在express中的应用,大部分示例直接来自于.morgan的文档:https://gi ...

  6. ubuntu下hive-0.8.1配置

    1.下载hive包wget http://labs.mop.com/apache-mirror/hive/stable/hive-0.8.1.tar.gz,并用tar -xzvf 将其解压到要安装的目 ...

  7. RecommenderFilterSalaryResult

    package org.andy.mymahout.recommendation.job; import java.io.BufferedReader; import java.io.File; im ...

  8. 网络应用(3):CDN与P2P的概念

    我前面说了流量的概念,流量是使用网络时经常要考虑的一个因素--如何才能更快的使用流量,如何才能节省流量使用的成本,对于这样的问题,你可能要了解一下什么是cdn,什么是p2p. (1)cdn是什么 cd ...

  9. 看一篇,学一篇,今日份的pandas,你该这么学!No.2

    开篇先嘚啵 昨天写到哪了? 睡醒就忘了... ... 不过聪明伶俐的博主,仅用1秒钟就想起来了 我们昨天学了一个pandas的类型series 并且会创建了,厉不厉害 对于一个新的数据结构来说 额,不 ...

  10. “Enterprise Architect”和数据库的不解之缘

    前言 在这个大数据盛行的时代,和数据打交道变的必不可少了,所有如果有工具来规范我们的数据库会更加方便我们的生活.这次机房,我们利用EA(Enterprise Architect)自动生成SQL语句来达 ...