C. Road Optimization(dp)

Problem - 1625C - Codeforces (Unofficial mirror site, accelerated for Chinese users)

C. Road Optimization
time limit per test3 seconds
memory limit per test128 megabytes
inputstandard input
outputstandard output
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 x (0≤x≤ℓ), which is equal to the distance from Kstolop in kilometers. So, Kstolop is located in the point with coordinate 0, and Olymp City is located in the point with coordinate ℓ.

There are n signs along the road, i-th of which sets a speed limit ai. This limit means that the next kilometer must be passed in ai 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 0), 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=47 minutes.

To optimize the road traffic, the Government of Mars decided to remove no more than k 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 n, ℓ, k (1≤n≤500, 1≤ℓ≤105, 0≤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 n integers di (d1=0, di<di+1, 0≤di≤ℓ−1) — coordinates of all signs.

The third line contains n integers ai (1≤ai≤10^4) — 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 k road signs.

#include<iostream>
#include<algorithm>
#include<string> 
#include<set> 
#include<map>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<unordered_map>
#include<iomanip>
#define ll long long
#define ull unsigned long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define m_p make_pair
#define pi acos(-1)
using namespace std;

const int N = 1e6 + 5;
const int M = 5e5 + 5;
const double eps = 1e-4;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;

ll n, l, k, dp[505][505], x[505], v[505];

void solve()
{
	memset(dp, inf ,sizeof dp);
	fill(dp[1], dp[1]+504, 0);
	cin>>n>>l>>k;
	for(ll i = 1; i <= n; i++)
		cin>>x[i];
	for(ll i = 1; i <= n; i++)
		cin>>v[i];
		
	x[++n] = l;
	for(ll i = 2; i <= n; i++)
	{
		for(ll j = i - 1; i - j - 1 <= k  ; j--)//j -> i之间的都删除 
		{
			for(ll p = 0; p + i - j - 1 <= k && p < j; p++)
			{
				dp[i][p + i - j - 1] = min(dp[i][p + i - j - 1], dp[j][p] + v[j] * (x[i]-x[j])); //从j直接到i,若到i的转移次数加上i到j的转移次数小于k的状态进行转移
			}
		}
	}
	ll ans = inf;
	for(ll i = 0; i <= k; i++)
		ans = min(ans, dp[n][i]);
	cout<<ans;
}

signed main()
{
	IOS;
	//unit();
	ll t = 1;
	//cin>>t;
	while(t--)
		solve();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值