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( ...
随机推荐
- Spring boot 学习二:入门
1: 需要的环境: JDK:至少JDK7才支持Spring boot maven:至少3.2 spring-boot:1.2.5.RELEASE(在pom.xml中指定) 2: 创建一个maven工程 ...
- javadoc 工具生成开发API文档
=====================先来一点成就感===================== package com.springMybatis.dao; import com.springMy ...
- 【总结整理】JavaScript的DOM事件学习(慕课网)
事件:在文档或者浏览器窗口中发生的一些,特定的交互瞬间 HTML和JavaScript的交互通过事件 来实现 比如:1.滚动条向下滑动,加载图片 2.图片轮播,鼠标由2-5页调换 本章内容1.理解事件 ...
- VMware安装Ubuntu时出现Intel VT-X处于禁用状态的情况的处理办法
VMware安装Ubuntu时出现Intel VT-X处于禁用状态的情况的处理办法 VMware安装Ubuntu的出现Intel VT-X处于禁用状态的情况会使已经安装好的Ubuntu虚拟机打不开 ...
- 第二篇elasticsearch配置
1.去github搜索 elashsearch——head,以mobz开头的2.在根目录下安装npm install 3.修改elashsearch下的config文件下的elashsearch.yu ...
- 业务逻辑:完成基于CRM地址完全匹配的自动分单业务逻辑
思路: 后台系统的业务接口服务处理接收到的数据并使用Webservice技术来远程调用CRM系统的业务接口服务来进行定区的查询操作,随后从该定区中匹配一个快递员来分配工单并发送短信通知取件 操作步骤: ...
- url中传中文
function gotoPage(code,name){//name为中文 window.location.href="upload.jsp?bigCategoryCode="+ ...
- 实用掌中宝--HTML&CSS常用标签速查手册 PDF扫描版
实用掌中宝--HTML&CSS常用标签速查手册 内容推荐: 本书第一篇以语法和实例相结合的形式,详细讲解了HTML语言中各个元素及其属性的作用.语法和显示效果:第二篇从CSS基本概念开始,分别 ...
- 【创建maven-web项目-eclipse-jee-mars-2-win32-x86_64-jdk1.8】
需要注意的是:创建的mavenweb项目是没有java源文件夹的需要手动添加: 创建项目完成以后,项目会报错,如下: 因为项目中没有添加servlet依赖解决jsp报错问题:添加依赖: <dep ...
- 该项目中不存在目标“GatherAllFilesToPublish”
在项目发布的时候报了一个 该项目中不存在目标“GatherAllFilesToPublish” 的奇怪错误,查了之后,在报错的.csproj里面添加 <Target Name="Ga ...