[Charging planning for expressway rest stations] c++

Source: http://t.csdn.cn/5fIdA
Title Description
Zhang San bought a self-driving new energy vehicle with a range of 1,000 kilometers. One day, after the vehicle is fully charged, it is necessary to start from City A to City B, which is D kilometers away, and take the high speed all the way.

The car navigation prompts that there are N rest stops along the way that can provide charging services, and each rest stop can provide the current charging queue time (hours) in real time.
Please help plan the optimal time-optimized rest stop charging scheme and return the shortest travel time.

For the convenience of calculation, the driving speed on the highway is fixed at 100 km/h. When planning, it is not necessary to consider the number of safe cruising ranges. The car can completely run out of electricity, and a car with a 1,000-kilometer cruising range can be driven at 100 km/h for 10 hours. The charging time is fixed at 1 hour each time, and the battery is fully charged after completion. The charging queuing time of each station will not change, and the charging queuing process does not consume power.

enter description

The first line indicates the distance D between A and B, and the unit is kilometers; the
second line indicates the number N of rest stops along the way;
from the third line, each line has 2 data, respectively indicating the distance between the rest stop and A city, and the charging queue Required time (hours), (the rest stops are sorted from near to far)
0<=D<=1000000, D is an integer multiple of 100
0<=N<=10000

1500
4
300 2
600 1
1000 0
1200 0

output description

The shortest time (hours) spent on the journey.
If the destination cannot be reached, -1 will be returned

16

Explanation:
The best solution: charging only at the third rest stop (position 1000) for a
1,500-kilometer journey: 15 hours,
charging queuing for 0 hours, and charging for 1 hour.
The fastest journey takes a total of 16 hours

Other plan: charge at the second rest stop (position 600), and spend a total of 17 hours
Other plans: charge at the second rest stop (position 600) and the fourth rest stop (position 1200), and spend a total of 19 hours

train of thought

#include<bits/stdc++.h>

using namespace std;

vector<int> power_dis;
vector<int> power_time;

unordered_map<string, int> memo;

int dp(int pow, int dis, int N) {
    
    
	// 
	if (pow >= dis) return dis / 100;
	//
	string key = to_string(pow) + "," + to_string(dis);
	cout << key << endl;
	if (memo.count(key)) return memo[key];
	int res = INT_MAX;
	int can = dis - pow;
	for (int i = 0; i < N; i++) {
    
    
		if (can > power_dis[i]) continue;
		if (power_dis[i] == dis) continue;
		int power_d = power_dis[i];
		int power_t = power_time[i];
		if (dp(1000, power_d, N) == -1) continue;
		int tmp = (dis - power_d) / 100 + power_t + 1 + dp(1000, power_d, N);
		res = min(res, tmp);
	}
	if (res == INT_MAX) res = -1;
	memo[key] = res;
	return res;
}


int main() {
    
    
	int D, n;
	cin >> D;
	cin >> n;
	power_dis.resize(n);
	power_time.resize(n);
	for (int i = 0; i < n; i++) cin >> power_dis[i] >> power_time[i];
	// base case
	for (int i = 0; i < n; i++) power_dis[i] = D - power_dis[i];
	cout << dp(1000, D, n);
	return 0;
}

Guess you like

Origin blog.csdn.net/LemonShy2019/article/details/126747376