poj 2465 Adventures in Moving - Part IV
来源:互联网 发布:dnf tp技能详细数据 编辑:程序博客网 时间:2024/05/19 12:26
题目大意:有一辆卡车,从一个城市A到达城市B,车的油箱为200,初始有100的燃料,每走一个单元路程消耗1个单元的燃料。
这里有几个加油站,每个加油站是按照距离A城市的距离从小到大排列的,每个加油站每单元的燃料的价格是不同的。
要你求卡车到终点加燃料的花费最小时多少,若卡车到不了终点,输出Impossible
解题思路:
动态规划,dp[i][j]表示行驶到第i个加油站,燃料为j是的最小花费
列出动态转移方程
dp[i][j] = min(dp[i-1][k ]+ rest * p[i]])
假设每个站距离A的距离为dist[i]
那么k 大于等于 dist[i] - dist[i-1]因为要保证第i-1的加油站能行驶到第i个加油站,p[i]为第i个加油站的燃料价格
k - (dist[i] - dist[i - 1])表示行驶到第i个加油站的剩余燃料量。
rest = j - ( k - (dist[i] - dist[i - 1])),表示在j站加了多少燃料
这里要考虑最后是否能到终点,所以A城市到B城市的距离d作为一个站,价格为无穷大。
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int maxn = 210;const int maxv = 20000000;struct node{int dist, price; };node station[maxn / 2];int dp[maxn / 2][maxn];int main(){int d;scanf("%d", &d);station[0].dist = station[0].price = 0;int index = 1;while(scanf("%d %d", &station[index].dist, &station[index].price) != EOF)index++;station[index].dist = d;station[index++].price = maxv;for(int i = 0; i < index; i++){for(int j = 0; j <= 200; j++)dp[i][j] = maxv;}dp[0][100] = 0;for(int i = 1; i < index; i++){for(int j = 0; j <= 200; j++){int st = station[i].dist - station[i - 1].dist;for(int k = st; k <= 200; k++){if(maxv == dp[i - 1][k])continue;if(k - st > j)break;int rest = j - (k - st);//要是最后得终点能到达,rest的结果会为0,dp[i][j]会取dp[i-1][k]里的最小一个//要是不能到达,dp[i - 1][k] + rest * station[i].price会略大于dp[i][j],dp[i][j]会为maxv;dp[i][j] = min(dp[i][j], dp[i - 1][k] + rest * station[i].price);}}}if(dp[index - 1][100] != maxv)printf("%d\n", dp[index - 1][100]);elseprintf("Impossible\n");return 0;}
- poj 2465 Adventures in Moving - Part IV
- POJ 2465 Adventures in Moving - Part IV
- 10201 - Adventures in Moving - Part IV
- uva 10201 Adventures in Moving - Part IV
- UVA 10201 Adventures in Moving - Part IV
- ZOJ1894 POJ2465 Adventures in Moving - Part IV
- UVA 10201 Adventures in Moving - Part IV
- UVA 10201 Adventures in Moving - Part IV
- 10201 - Adventures in Moving - Part IV
- uva 10201 Adventures in Moving - Part IV
- UVA - 10201 Adventures in Moving - Part IV
- uva10201 - Adventures in Moving - Part IV
- UVA10201 - Adventures in Moving - Part IV
- uva 10201 - Adventures in Moving - Part IV
- Adventures in Moving - Part IV+uva+dp
- UVA - 10201 Adventures in Moving - Part IV
- UVA - 10201 Adventures in Moving - Part IV
- UVA - 10201 Adventures in Moving - Part IV
- 2489 Minimal Ratio Tree 最小生成树(选边)
- 关于sort命令排序技巧
- Spring MVC 3 深入总结
- FS_S5PC100平台上Android Camera架构分析
- 慢慢发现,我在退步
- poj 2465 Adventures in Moving - Part IV
- 配置IIS支持asp.net
- 关于strcpy_s源代码的问题
- 【转】Spring @Transactional 声明式事务管理 getCurrentSession
- php _REQUEST变量的使用
- resin的watchdog
- 软件工程师在职业生涯中说的32句话
- AO+C++实现唯一值渲染
- VIM 操作