UVA - 10201 Adventures in Moving - Part IV

题目大意:有n个测试数据组, 对于每个测试组,最先给出一个距离lenth, 然后给出若干个加油站的位置以及加油站每升油的价钱。然后有量油桶容量为200升的卡车,出距离为0的位置开始移动向lenth,每升油可以使的卡车走一个单位距离,问,卡车到达lenth的时候,并且油箱中仍有100升油,最少花费多少钱,如果不能到达,输出“Impossible”。


解题思路:用递推的方法,对每个加油站进行处理,dp[i][j],i代表第i个加油站,j表示油箱有多少升油。j的范围为0~200。然后除了起点之外,其他的油箱最大值为200,如果s[i].dis - s[i - 1].dis > top, 说明在中间的路程会出现断油,到达不了终点,并且dp[i][j]的最小值有两种来源,一种是从前一个加油站剩余的油,另一种是在本站加油。

<span style="font-size:18px;">#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int main() {
	int  T, end, DP[105][205], D[105] = {0}, P[105] = {0};
	scanf("%d%*C",&T);
	while(T--) {
		scanf("%d%*C", &end); 
		int n = 1;
		char line[50];
		while(gets(line) && line[0]) {
			sscanf(line,"%d%d", &D[n], &P[n]);
			if (D[n] > end) break;
			n++;
		}
		D[n] = end;
		memset(DP, 0x3f3f3f3f, sizeof(DP));
		DP[0][100] = 0;

		for(int i = 1; i <= n; i++) {
			int dis = D[i] - D[i-1];
			for(int j = dis; j <= 200; j++)
				DP[i][j-dis] = DP[i-1][j];

			if (i == n) break;
			for(int j = 1; j <= 200; j++) {
				DP[i][j] = min(DP[i][j], DP[i][j-1] + P[i]);
			}
		}
		if (DP[n][100] != 0x3f3f3f3f)
			printf("%d\n", DP[n][100]);
		else
			printf("Impossible\n");
		if (T) 
			printf("\n");
	}
	return 0;
}
</span>


你可能感兴趣的:(UVA - 10201 Adventures in Moving - Part IV)