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( ...
随机推荐
- QTP使用outlook发送邮件
'发邮件 Dim objOutlook Dim objOutlookMsg Dim olMailItem ' Create the Outlook object and the new mail ...
- mycat的安装与配置
前提:mycat安装前需先实现主从复制,主从复制不会的,请看 文章 .另外,配置前需关掉selinux. 一.此次测试环境总共有四台机: mycat: 10.0.0.20 mysql_master: ...
- 树莓派 Learning 002 必备的操作 --- 08 实现PC端 远程登入 树莓派 --- 法1 远程登入树莓派的命令行状态
树莓派 必备的操作 - 实现PC端 远程登入 树莓派 - 法1 远程登入树莓派的命令行状态 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 ...
- java连接sqlserver2005数据库
java连接sqlserver2005数据库 首先得下载驱动程序到微软网站下载Microsoft JDBC Driver 4.0 for SQL Server 下载地址 :http://msdn. ...
- 14、/proc/cpuinfo 文件(查看CPU信息)
转载http://www.cnblogs.com/itcomputer/p/4888438.html /proc/cpuinfo文件分析 根据以下内容,我们则可以很方便的知道当前系统关于CPU.CPU ...
- Entity Framework Code-First(9.11):DataAnnotations - InverseProperty Attribute
DataAnnotations - InverseProperty Attribute: We have seen in the Code-First Convention section that ...
- byte和int转换
byte b1=1,b2=2,b3,b6; final byte b4=4,b5=6; b6=b4+b5; b3=(b1+b2); System.out.println(b3+b6); b3=b1+b ...
- Spring入门第二课
看代码 package logan.spring.study; public class HelloWorld { private String name; public void setName2( ...
- 开源库ActiveAndroid + gson使用
ActiceAndroid的简介 ActiveAndroid是一个活跃的记录风格的ORM(对象关系映射)库.ActiveAndroid可以让您保存和检索的SQLite数据库记录而没有写一个SQL语句. ...
- 【转-mysql-explain介绍】
explain显示了MySQL如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 先解析一条sql语句,看出现什么内容 EXPLAINSELECTs.uid, ...