uva 10201 Adventures in Moving - Part IV (DP)
来源:互联网 发布:买汉服哪家店淘宝 编辑:程序博客网 时间:2024/05/29 14:47
uva 10201 Adventures in Moving - Part IV
题目大意:借了一辆车,车里有100单位的油。要到达N米外的目的地(每走一米消耗一个单位的油),在这一段路程中,有若干个加油站,给出的数据是每个加油站的位置和加一单位油的价格。要求到达目的地且剩下100单位油的最小消费。(到达不了则输出Impossible)
解题思路:dp[i][j]数组代表的是第i个加油站油量为j的最小费用。
状态转移方程:
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#define N 2005#define M 205using namespace std;typedef long long ll;const int INF = 1000000000;struct GAS{ int mile, money;}g[N];int cmp(GAS a, GAS b) { return a.mile < b.mile;}char s[100];int n, cnt, dp[N][M];void DP() { for (int i = 1; i <= cnt; i++) { int dis = g[i].mile - g[i - 1].mile; for (int j = 0; j + dis <= 200; j++) { if (dp[i - 1][j + dis] < dp[i][j]) { dp[i][j] = dp[i - 1][j + dis]; } } for (int j = 1; j <= 200; j++) { int temp = INF; for (int k = 0; k < j; k++) { int sum = dp[i][k] + (j - k) * g[i].money; if (sum < temp) temp = sum; } if (temp < dp[i][j]) { dp[i][j] = temp; } } }}int main() { int T; scanf("%d\n", &T); while (T--) { cnt = 0; scanf("%d%*c", &n); while (gets(s) != NULL && s[0] != 0) { ++cnt; sscanf(s, "%d %d", &g[cnt].mile, &g[cnt].money); if (g[cnt].mile < 0 || g[cnt].mile > n) cnt--; } g[0].mile = 0; for (int i = 0; i <= cnt; i++) { for (int j = 0; j <= 200; j++) { dp[i][j] = INF; } } dp[0][100] = 0; DP(); if (n - g[cnt].mile > 100 || dp[cnt][100 + n - g[cnt].mile] == INF) { printf("Impossible\n"); } else { printf("%d\n", dp[cnt][100 + n - g[cnt].mile]); } if (T) printf("\n"); } return 0;}
0 0
- uva 10201 Adventures in Moving - Part IV(dp)
- uva 10201 Adventures in Moving - Part IV (DP)
- Adventures in Moving - Part IV+uva+dp
- UVA 10201 Adventures in Moving - Part IV(dp)
- uva 10201 - Adventures in Moving - Part IV(dp)
- UVA 10201 Adventures in Moving - Part IV 车站加油 dp
- Adventures in Moving - Part IV - UVa 10201 dp
- uva 10201 Adventures in Moving - Part IV
- UVA 10201 Adventures in Moving - Part IV
- UVA 10201 Adventures in Moving - Part IV
- UVA 10201 Adventures in Moving - Part IV
- uva 10201 Adventures in Moving - Part IV
- UVA - 10201 Adventures in Moving - Part IV
- uva 10201 - Adventures in Moving - Part IV
- UVA - 10201 Adventures in Moving - Part IV
- UVA - 10201 Adventures in Moving - Part IV
- UVA - 10201 Adventures in Moving - Part IV
- uva 10201 Adventures in Moving - Part IV
- 运动估计——块匹配算法
- [BZOJ1305]CQOI2009跳舞|最大流判定
- 刘强东哈佛演讲:解决一个问题,创业就能成功
- C++虚基类的实现机制
- UVa 10534 - Wavio Sequence (最长上升子序列LIS)
- uva 10201 Adventures in Moving - Part IV (DP)
- Rainy season best wbp brown film faced plywood
- [BZOJ2435]NOI2011道路修建|dfs|bfs
- iOS多线程编程之NSThread的使用
- 04-树5. Complete Binary Search Tree
- HDU5206-Four Inages Strategy
- Q: android R.java资源文件不自动生成
- Java读书笔记02 基础知识
- Light oj 1038 Race to 1 Again(概率dp)