汽车加油行驶问题专题

95 篇文章 0 订阅
73 篇文章 0 订阅

Example  one:

Link:http://poj.org/problem?id=2431

Problem:

Expedition
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7384 Accepted: 2190

Description

A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck's fuel tank. The truck now leaks one unit of fuel every unit of distance it travels. 

To repair the truck, the cows need to drive to the nearest town (no more than 1,000,000 units distant) down a long, winding road. On this road, between the town and the current location of the truck, there are N (1 <= N <= 10,000) fuel stops where the cows can stop to acquire additional fuel (1..100 units at each stop). 

The jungle is a dangerous place for humans and is especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the way to the town. Fortunately, the capacity of the fuel tank on their truck is so large that there is effectively no limit to the amount of fuel it can hold. The truck is currently L units away from the town and has P units of fuel (1 <= P <= 1,000,000). 

Determine the minimum number of stops needed to reach the town, or if the cows cannot reach the town at all. 

Input

* Line 1: A single integer, N 

* Lines 2..N+1: Each line contains two space-separated integers describing a fuel stop: The first integer is the distance from the town to the stop; the second is the amount of fuel available at that stop. 

* Line N+2: Two space-separated integers, L and P

Output

* Line 1: A single integer giving the minimum number of fuel stops necessary to reach the town. If it is not possible to reach the town, output -1.

Sample Input

4
4 4
5 2
11 5
15 10
25 10

Sample Output

2

Hint

INPUT DETAILS: 

The truck is 25 units away from the town; the truck has 10 units of fuel. Along the road, there are 4 fuel stops at distances 4, 5, 11, and 15 from the town (so these are initially at distances 21, 20, 14, and 10 from the truck). These fuel stops can supply up to 4, 2, 5, and 10 units of fuel, respectively. 

OUTPUT DETAILS: 

Drive 10 units, stop to acquire 10 more units of fuel, drive 4 more units, stop to acquire 5 more units of fuel, then drive to the town.

Source


Problem  solution:greedy  algorithm。

思想:贪心。就是每经过一个加油站,就把该加油站存起来,因为要求求出次数最少,所以每次加油要尽可能的多。这里用到C++中STL的优先队列实现当汽车油为0的时候,总是选择前面经过的加油站能够提供最多油的加油站进行加油,如果此时队列为空,说明无法到达下一站,输出"-1",否则加油次数加1。注意要把终点当成是最后一个加油站。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
const int maxn=10011;
struct node{
	int d;
	int f;
	bool operator <(const node &a)const
	{
		return f<a.f;
	}
}stop[maxn];
priority_queue<node>Q;
bool cmp(node a,node b)
{
	return a.d<b.d;
}
int main()
{
	int i,ans,n,L,p,dd,fg;
	while(scanf("%d",&n)==1)
	{
		for(i=0;i<n;i++)
		{
			scanf("%d%d",&stop[i].d,&stop[i].f);
		}
		scanf("%d%d",&L,&p);
		for(i=0;i<n;i++)
		{
			stop[i].d=L-stop[i].d;
		}
		stop[n].d=L;
		stop[n].f=0;
		sort(stop,stop+n+1,cmp);
		ans=0;
		dd=0;
		fg=1;
		while(!Q.empty())
		Q.pop();
		for(i=0;i<=n;i++)
		{
			if(p>=stop[i].d-dd)
			{
				p-=stop[i].d-dd;
				Q.push(stop[i]);
				dd=stop[i].d;
			}
			else
			{
				while(p<stop[i].d-dd)
				{
					if(Q.empty())
					{
						ans=-1;
						fg=0;
						break;
					}
					p+=Q.top().f;
					ans++;
					Q.pop();
				}
				if(fg)
				{
				p-=stop[i].d-dd;
				dd=stop[i].d;
				Q.push(stop[i]);
			    }
			    else
			    break;
			}
		}
		printf("%d\n",ans);
	}
}


Example  one:

Link:http://poj.org/problem?id=2431

Problem:



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1.问题描述 给定一个N*N 的方形网格,设其左上角为起点,坐标为(1,1),X 轴向右为正,Y 轴 向下为正,每个方格边长为1。一辆汽车从起点出发驶向右下角终点,其坐标为(N,N)。 在若干个网格交叉点处,设置了油库,可供汽车行驶途中加油汽车行驶过程中应遵守 如下规则: (1)汽车只能沿网格边行驶,装满油后行驶K 条网格边。出发时汽车已装满油,在 起点与终点处不设油库。 (2)当汽车行驶经过一条网格边时,若其X 坐标或Y 坐标减小,则应付费用B,否则 免付费用。 (3)汽车行驶过程中遇油库则应加满油并付加油费用A。 (4)在需要时可在网格点处增设油库,并付增设油库费用C(不含加油费用A)。 (5)(1)~(4)中的各数N、K、A、B、C均为正整数。 算法设计: 求汽车从起点出发到达终点的一条所付费用最少的行驶路线。 数据输入: 输入数据。第一行是N,K,A,B,C的值,2 <= N <= 100, 2 <= K <= 10。第二行起是一个N*N 的0-1方阵,每行N 个值,至N+1行结束。方阵的第i 行第j 列处的值为1 表示在网格交叉点(i,j)处设置了一个油库,为0 时表示未设油库。 各行相邻的2 个数以空格分隔。 结果输出: 将找到的最优行驶路线所需的费用,即最小费用输出. Sample input 9 3 2 3 6 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 1 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1 1 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 1 0 0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 Sample output 12
动态规划是一种常用的优化方法,可以用来解决汽车加油行驶问题。在这个问题中,我们需要确定汽车在一条路上加油的最佳策略,以使其能够行驶最远的距离。 下面是使用动态规划解决汽车加油行驶问题的一般步骤: 1. 定义状态:首先,我们需要定义问题的状态。在这个问题中,一个可能的状态可以是汽车当前所在的位置和已经加过的油量。 2. 确定状态转移方程:接下来,我们需要确定状态之间的转移关系。假设当前状态为(i, j),表示汽车当前在位置i,并且已经加过j单位的油量。那么,下一个状态可以是(i+1, j+k),其中k表示在位置i+1加的油量。我们需要遍历所有可能的加油量k,并选择能够使汽车行驶最远距离的加油量。 3. 初始化边界条件:我们需要初始化边界条件,即确定起始状态和结束状态。起始状态可以是(0, 0),表示汽车从起点出发时没有加过油。结束状态可以是(n, j),其中n表示终点位置,j表示加油量。 4. 递推计算最优解:通过递推计算,我们可以得到从起点到终点的最优解。具体的计算方法可以使用一个二维数组dp来保存每个状态的最优解,其中dp[i][j]表示在位置i并且加过j单位油量时能够行驶的最远距离。通过状态转移方程和边界条件,我们可以逐步计算出dp数组中的每个元素。 5. 返回结果:最后,我们可以返回dp[n][j],即在终点位置并且加过j单位油量时能够行驶的最远距离。 下面是一个使用动态规划解决汽车加油行驶问题C++代码示例: ```cpp #include <iostream> #include <vector> using namespace std; int maxDistance(int n, int fuel, vector<int>& stations) { vector<vector<int>> dp(n + 1, vector<int>(fuel + 1, -1)); dp = 0; for (int i = 0; i < n; i++) { for (int j = 0; j <= fuel; j++) { if (dp[i][j] >= 0) { for (int k = 0; k <= fuel - j; k++) { if (j + k >= stations[i]) { dp[i + 1][j + k - stations[i]] = max(dp[i + 1][j + k - stations[i]], dp[i][j] + 1); } } } } } int maxDist = -1; for (int j = 0; j <= fuel; j++) { maxDist = max(maxDist, dp[n][j]); } return maxDist; } int main() { int n = 5; // 路的长度 int fuel = 10; // 初始油量 vector<int> stations = {2, 4}; // 加油站的位置 int maxDist = maxDistance(n, fuel, stations); cout << "汽车能够行驶的最远距离为:" << maxDist << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值