我是靠谱客的博主 体贴期待,最近开发中收集的这篇文章主要介绍动态规划dp,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

You are planning to buy an apartment in a nn-floor building. The floors are numbered from 11 to nn from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.

Let:

aiai for all ii from 11 to n−1n−1 be the time required to go from the ii-th floor to the (i+1)(i+1)-th one (and from the (i+1)(i+1)-th to the ii-th as well) using the stairs;
bibi for all ii from 11 to n−1n−1 be the time required to go from the ii-th floor to the (i+1)(i+1)-th one (and from the (i+1)(i+1)-th to the ii-th as well) using the elevator, also there is a value cc — time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!).
In one move, you can go from the floor you are staying at xx to any floor yy (x≠yx≠y) in two different ways:

If you are using the stairs, just sum up the corresponding values of aiai. Formally, it will take ∑i=min(x,y)max(x,y)−1ai∑i=min(x,y)max(x,y)−1ai time units.
If you are using the elevator, just sum up cc and the corresponding values of bibi. Formally, it will take c+∑i=min(x,y)max(x,y)−1bic+∑i=min(x,y)max(x,y)−1bi time units.
You can perform as many moves as you want (possibly zero).

So your task is for each ii to determine the minimum total time it takes to reach the ii-th floor from the 11-st (bottom) floor.

Input
The first line of the input contains two integers nn and cc (2≤n≤2⋅105,1≤c≤10002≤n≤2⋅105,1≤c≤1000) — the number of floors in the building and the time overhead for the elevator rides.

The second line of the input contains n−1n−1 integers a1,a2,…,an−1a1,a2,…,an−1 (1≤ai≤10001≤ai≤1000), where aiai is the time required to go from the ii-th floor to the (i+1)(i+1)-th one (and from the (i+1)(i+1)-th to the ii-th as well) using the stairs.

The third line of the input contains n−1n−1 integers b1,b2,…,bn−1b1,b2,…,bn−1 (1≤bi≤10001≤bi≤1000), where bibi is the time required to go from the ii-th floor to the (i+1)(i+1)-th one (and from the (i+1)(i+1)-th to the ii-th as well) using the elevator.

Output
Print nn integers t1,t2,…,tnt1,t2,…,tn, where titi is the minimum total time to reach the ii-th floor from the first floor if you can perform as many moves as you want.

Examples
Input
10 2
7 6 18 6 16 18 1 17 17
6 9 3 10 9 1 10 1 5
Output
0 7 13 18 24 35 36 37 40 45
Input
10 1
3 2 3 1 3 3 1 4 1
1 2 3 4 4 1 2 1 3
Output
0 2 4 7 8 11 13 14 16 17
大致意思是:

第一行数据有n层楼,等电梯需要时间T,第二行数据为从走楼梯从第i层到i+1层所需要的时间,第三行数据为坐电梯从从第i层到i+1层所需要的时间;。求从1楼到 1 ~ n 层楼所需要的最短时间

思路:从第i层楼到第i+1层楼有下列四种走法:

1.楼梯–>楼梯
2.楼梯–>电梯
3.电梯–>楼梯
4.电梯–>电梯

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,t,a[200001],dp[200001][2],b[200001];
int main()
{

	scanf("%d%d",&n,&t);
	int i,f=0;
	for(i=1;i<n;i++)
	scanf("%d",&a[i]);
	for(i=1;i<n;i++)
	{
		scanf("%d",&b[i]);
	}
	dp[1][0]=0;
	dp[1][1]=t; 
    for(i=1;i<n;i++)
    {
        dp[i+1][0]=min(dp[i][1]+a[i],dp[i][0]+a[i]);
        dp[i+1][1]=min(dp[i][1]+b[i],dp[i][0]+b[i]+t);
    }
    for(i=1;i<=n;i++)
    printf("%d ",min(dp[i][0],dp[i][1]));
    printf("n");
}

最后

以上就是体贴期待为你收集整理的动态规划dp的全部内容,希望文章能够帮你解决动态规划dp所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部