UVALive 4261——Trip Planning——————【dp+打印路径】
2015-10-17 10:27
459 查看
Trip Planning
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Submit Status Practice UVALive 4261
Description
![](https://icpcarchive.ecs.baylor.edu/components/com_onlinejudge/images/button_pdf.png)
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
Sample Output
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Submit Status Practice UVALive 4261
Description
![](https://icpcarchive.ecs.baylor.edu/components/com_onlinejudge/images/button_pdf.png)
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 ); } printf("p=%lld\n",dp ); 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; }
相关文章推荐
- spring 配置文件
- java.sql.SQLException: Parameter number X is not an OUT parameter
- Binary Tree Maximum Path Sum
- nginx 配置文件
- 软件测试
- Cocos2d-x能够实现的效果总结
- 有意思的时钟
- http 中get post put delete
- 搭建流水线
- ASP.NET MVC4中使用bootstrip模态框时弹不出的问题
- 浅析Date与Java 时间戳相互转换
- spring三种数据源方式
- VS2008 SP1开发环境补丁列表
- C++中delete和delete[]的区别 http://www.cnblogs.com/charley_yang/archive/2010/12/08/1899982.html
- 感恩篇
- 从视频文件中读入数据-->将数据转换为灰度图-->对图像做canny边缘检测-->将这三个结构显示在一个图像中
- 网站发的文章有收录 但是没有排名怎么处理
- c# 计时器的三种使用方法
- vim集成开发环境搭建之ctags配置使用
- Cocos2d-x实现用户触摸的功能