C. Road Optimization(dp)

12 篇文章 0 订阅

The Government of Mars is not only interested in optimizing space flights, but also wants to improve the road system of the planet.

One of the most important highways of Mars connects Olymp City and Kstolop, the capital of Cydonia. In this problem, we only consider the way from Kstolop to Olymp City, but not the reverse path (i. e. the path from Olymp City to Kstolop).

The road from Kstolop to Olymp City is ℓℓ kilometers long. Each point of the road has a coordinate xx (0≤x≤ℓ0≤x≤ℓ), which is equal to the distance from Kstolop in kilometers. So, Kstolop is located in the point with coordinate 00, and Olymp City is located in the point with coordinate ℓℓ.

There are nn signs along the road, ii-th of which sets a speed limit aiai. This limit means that the next kilometer must be passed in aiai minutes and is active until you encounter the next along the road. There is a road sign at the start of the road (i. e. in the point with coordinate 00), which sets the initial speed limit.

If you know the location of all the signs, it's not hard to calculate how much time it takes to drive from Kstolop to Olymp City. Consider an example:

Here, you need to drive the first three kilometers in five minutes each, then one kilometer in eight minutes, then four kilometers in three minutes each, and finally the last two kilometers must be passed in six minutes each. Total time is 3⋅5+1⋅8+4⋅3+2⋅6=473⋅5+1⋅8+4⋅3+2⋅6=47 minutes.

To optimize the road traffic, the Government of Mars decided to remove no more than kk road signs. It cannot remove the sign at the start of the road, otherwise, there will be no limit at the start. By removing these signs, the Government also wants to make the time needed to drive from Kstolop to Olymp City as small as possible.

The largest industrial enterprises are located in Cydonia, so it's the priority task to optimize the road traffic from Olymp City. So, the Government of Mars wants you to remove the signs in the way described above.

Input

The first line contains three integers nn, ℓℓ, kk (1≤n≤5001≤n≤500, 1≤ℓ≤1051≤ℓ≤105, 0≤k≤n−10≤k≤n−1), the amount of signs on the road, the distance between the cities and the maximal number of signs you may remove.

The second line contains nn integers didi (d1=0d1=0, di<di+1di<di+1, 0≤di≤ℓ−10≤di≤ℓ−1) — coordinates of all signs.

The third line contains nn integers aiai (1≤ai≤1041≤ai≤104) — speed limits.

Output

Print a single integer — minimal possible time to drive from Kstolop to Olymp City in minutes, if you remove no more than kk road signs.

题意:有一条长为l的路,有n个路标,每个路标i有位置坐标d[i]和到下一个路标的路程里每公里所需的时间a[i],求最多能移除k个路标的所用的最短时间。(位于坐标为0的路标不能移除)

判断是不是dp:移走第i个路标的最佳方案是不是在移走第i-1个路标的方案的基础上,如果不是就是dp,是就是贪心

思路:

用dp[i][0][j]来表示到第i+1个路标,已经移除了j个路标,而且第i个路标被移除了的最短时间

用dp[i][1][j]来表示到第i+1个路标,已经移除了j个路标,而且第i个路标没有被移除了的最短时间

我们要求最短时间必须要找出来最后一个没有被移除的路标,这样才能算出到下一个坐标所花费的时间(没有移除的坐标的a[i]*他们之间的距离)

dp[i][1][j]的没有被移除的路标就是i,那么他的状态转移方程就是到达第i个路标所需的最短时间(第i-1个路标有没有被移除)再加上到第i+1个路标所需的时间(a[i]*(d[i+1]-d[i]))

dp[i][1][j]=min(dp[i-1][0][j],dp[i-1][1][j])+a[i]*(d[i+1]-d[i]);

对dp[i][0][j]来说,i已经移除了,我们没找到最后一个被移除的路标,所以我们要列举i之前的所有坐标找最后一个没有被移除的坐标l,算出花费时间的最小值

花费的时间是min(dp[ l ][ 1 ][ j - ( i - l )]+a[ l ]*(d[l+1]-d[i+1]));(因为最后一个没有被移除的路标如果是第l个的话,那么从l到i的所有路标都会被移除,那么后面就移除了(i-l)个路标,那么到l这的话是移除了j-(i-l)个路标)

初始化:因为第0个位置的路标不能删除,所以我们初始化dp[0][1][0]=a[0]*(d[1]-d[0]),剩下的都是正无穷就行了。注意加上最后一个坐标a[n]=l。

最后求答案的时候,因为是最多移除k个,我们要列举移除0~k个路标的情况取最小值就可以了

/*

 .----------------.  .----------------.  .----------------.  .----------------. 
| .--------------. || .--------------. || .--------------. || .--------------. |
| |  ________    | || |  _________   | || | ____    ____ | || |     ____     | |
| | |_   ___ `.  | || | |_   ___  |  | || ||_   \  /   _|| || |   .'    `.   | |
| |   | |   `. \ | || |   | |_  \_|  | || |  |   \/   |  | || |  /  .--.  \  | |
| |   | |    | | | || |   |  _|  _   | || |  | |\  /| |  | || |  | |    | |  | |
| |  _| |___.' / | || |  _| |___/ |  | || | _| |_\/_| |_ | || |  \  `--'  /  | |
| | |________.'  | || | |_________|  | || ||_____||_____|| || |   `.____.'   | |
| |              | || |              | || |              | || |              | |
| '--------------' || '--------------' || '--------------' || '--------------' |
 '----------------'  '----------------'  '----------------'  '----------------'

*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<deque>
#include<cmath>
#include<unordered_map>
#include<unordered_set>
#include<stack>
#define int long long
#define lowbit(x) x&(-x)
#define PI 3.1415926535
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int gcd(int a,int b){
	return b>0 ? gcd(b,a%b):a;
}
/*
int dx[8]={-2,-2,-1,1,2,2,-1,1};
int dy[8]={-1,1,2,2,1,-1,-2,-2};
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
int dx[8]={-1,1,0,0,-1,-1,1,1};
int dy[8]={0,0,-1,1,-1,1,-1,1};
*/
/*void init(){
	int con=0;
	for(int i=1;i<=15;i++)con+=i;
}
*/
/*
void sjdhfb(){
	int djbhff=0;
	for(int dd=1;dd<=4;dd++){
		djbhff+=dd;
	}
}
*/
//int e[N],ne[N],h[N],idx,w[N];
/*void add(int a,int b,int c){
	e[idx]=b;
	w[idx]=c;
	ne[idx]=h[a];
	h[a]=idx++;
}
*/
const int N=500+10;
int n,x,k;
int a[N],d[N];
int dp[N][2][N];
void sove(){
	cin>>n>>x>>k;
	d[n]=x;//在第n个位置的坐标为x
	for(int i=0;i<n;i++)cin>>d[i];
	for(int i=0;i<n;i++)cin>>a[i];
	memset(dp,0x3f,sizeof dp);
	dp[0][1][0]=a[0]*(d[1]-d[0]);
	for(int i=1;i<n;i++){
		for(int j=0;j<=k;j++){
			dp[i][1][j]=min(dp[i-1][0][j],dp[i-1][1][j])+a[i]*(d[i+1]-d[i]);//第i个被拆了的最短时间是前一段的最短时间+后面的时间
			for(int l=0;l<i;l++){
				if(j>=(i-l))dp[i][0][j]=min(dp[i][0][j],dp[l][1][j-(i-l)]+a[l]*(d[i+1]-d[l+1]));
			}
		}
	}
	int ans=0x3f3f3f3f;
	for(int i=0;i<=k;i++){
		ans=min(ans,min(dp[n-1][0][i],dp[n-1][1][i]));
	}
	cout<<ans<<endl;
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie() ,cout.tie() ;
	int t=1;
//	cin>>t;
//	init();
//	sjdhfb();
//	init();
	while(t--){
		sove();
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值