UVALive 4261——Trip Planning——————【dp+打印路径】
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 a1, a2,..., 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+打印路径】的更多相关文章
- 【noi 2.6_2000】&【poj 2127】 最长公共子上升序列 (DP+打印路径)
由于noi OJ上没有Special Judge,所以我是没有在这上面AC的.但是在POJ上A了. 题意如标题. 解法:f[i][j]表示a串前i个和b串前j个且包含b[j]的最长公共上升子序列长度 ...
- UVa11404 - Palindromic Subsequence(区间DP+打印路径)
题目大意 给定一个字符串,要求你删除尽量少的字符,使得原字符串变为最长回文串,并把回文串输出,如果答案有多种,则输出字典序最小的 题解 有两种解法,第一种是把字符串逆序,然后求两个字符串的LCS,并记 ...
- John's trip(POJ1041+欧拉回路+打印路径)
题目链接:http://poj.org/problem?id=1041 题目: 题意:给你n条街道,m个路口,每次输入以0 0结束,给你的u v t分别表示路口u和v由t这条街道连接,要输出从起点出发 ...
- POJ 1141 Brackets Sequence(区间DP, DP打印路径)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- UVALive 4262——Trip Planning——————【Tarjan 求强连通分量个数】
Road Networks Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Stat ...
- UVA 531 - Compromise(dp + LCS打印路径)
Compromise In a few months the European Currency Union will become a reality. However, to join th ...
- FatMouse's Speed ~(基础DP)打印路径的上升子序列
FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take ...
- PACM Team(牛客第三场多校赛+dp+卡内存+打印路径)
题目链接(貌似未报名的不能进去):https://www.nowcoder.com/acm/contest/141/A 题目: 题意:背包题意,并打印路径. 思路:正常背包思路,不过五维的dp很容易爆 ...
- UVA 1626 区间dp、打印路径
uva 紫书例题,这个区间dp最容易错的应该是(S)这种匹配情况,如果不是题目中给了提示我就忽略了,只想着左右分割忘记了这种特殊的例子. dp[i][j]=MIN{dp[i+1][j-1] | if( ...
随机推荐
- VisualGDB系列4:概述-Linux程序与VS
根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文将会阐述如何使用VisualGDB来 ...
- VMware Player 与 Device/Credential Guard 不兼容
一.问题 当前:使用VMware Workstation 操作:安装docker 结果:电脑重启后,无法打开VMware Wokstation中的虚拟机 错误提示: 二.解决方案: 1. 控制面板-- ...
- Linux下统计代码行数
使用wc统计代码行数 最近写了一些代码,想统计一下代码的行数,在eclipse中好像没这功能,网上搜了一下才发现原来Linux有一个统计文件行数的命令wc.使用wc可以打印出每个文件和总文件的行数.字 ...
- C语言连接mysql -select
C语言实现查询mysql数据库的行数,列的属性,以及每条记录. /* select.c */ #include <stdio.h> #include <stdlib.h> #i ...
- C#笔记(二)
转换操作符:操作符重载,可自定义实现从一种类型到另一种类型的显示或者隐式转换 : true/false也可进行操作符重载: LINQ中大部分查询运算符都有一个非常重要的特性:延迟执行.这意味着,他们不 ...
- 生物数据库介绍——NCBI
NCBI(National Center for Biotechnology Information,美国国家生物技术信息中心)除了维护GenBank核酸序列数据库外,还提供数据分析和检索资源.NCB ...
- IFrame与window对象(contentWindow)
ref:http://blog.csdn.net/dongzhiquan/article/details/5851201 var detialIframe=document.all("det ...
- 9.利用msfvenom生成木马
这篇文章来介绍一下msf中一个生成木马的msfvenom模块. msfvenom命令行选项如下: 英文原版: 中文版: Options: -p, --payload <payload> 指 ...
- hdu1079
#include<cstdio> #include<iostream> #include<cstring> using namespace std; int mai ...
- Android常见内存泄露,学会这六招优化APP性能
很多开发者都知道,在面试的时候会经常被问到内存泄露和内存溢出的问题. 1.内存溢出(Out Of Memory,简称 OOM),通俗理解就是内存不够,即内存占用超出内存的空间大小. 2.内存泄漏(Me ...