UVA10201 - Adventures in Moving - Part IV
2014-07-18 21:29
337 查看
题意:有一辆车,原始装有100汽油,到达距离为n的目的地,中间有cnt个加油站,每升油的价格为price[i]。每一公里耗油1,求到达目的地油箱仍然有100的最小花费。
思路:
d[i][j]表示在第i个加油站油箱有j升油的最小花费,k表示在前一个加油站所加的油, dis为i-1到i的距离.
1. 在i站不加油 d[i][j] = d[i - 1][j + dis];
2. 在i站加k升油 d[i][j] = min(d[i][j], d[i - 1][j - k + dis] + k * price[i]);
初始化,d[0][100] = 0。
注意最后抵达终点站时汽车本身要剩下100的汽油。
思路:
d[i][j]表示在第i个加油站油箱有j升油的最小花费,k表示在前一个加油站所加的油, dis为i-1到i的距离.
1. 在i站不加油 d[i][j] = d[i - 1][j + dis];
2. 在i站加k升油 d[i][j] = min(d[i][j], d[i - 1][j - k + dis] + k * price[i]);
初始化,d[0][100] = 0。
注意最后抵达终点站时汽车本身要剩下100的汽油。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 10005; int n, cnt; int city[MAXN], price[MAXN], d[MAXN][205]; char str[MAXN]; void dp() { for (int i = 0; i < cnt; i++) for (int j = 0; j <= 200; j++) d[i][j] = INF; d[0][100] = 0;a for (int i = 1; i < cnt; i++) { int dis = city[i] - city[i - 1]; for (int j = 0; j + dis <= 200; j++) if (d[i - 1][j + dis] != INF) d[i][j] = d[i - 1][j + dis]; for (int j = 0; j <= 200; j++) for (int k = 0; k <= j; k++) { if (j + dis - k <= 200 && d[i - 1][j + dis - k] != INF) d[i][j] = min(d[i][j], d[i - 1][j + dis - k] + k * price[i]); } } } int main() { int cas; scanf("%d", &cas); getchar(); while (cas--) { memset(city, 0, sizeof(city)); memset(price, 0, sizeof(price)); scanf("%d%*c", &n); cnt = 1; while (gets(str) != NULL && str[0] != '\0') { sscanf(str, "%d %d", &city[cnt], &price[cnt]); cnt++; } dp(); if (100 < n - city[cnt - 1] || d[cnt - 1][100 + n - city[cnt - 1]] == INF) printf("Impossible\n"); else printf("%d\n", d[cnt - 1][100 + n - city[cnt - 1]]); if (cas) printf("\n"); } return 0; }
相关文章推荐
- UVA 10201 Adventures in Moving - Part IV(动态规划)
- uva 10201 Adventures in Moving - Part IV (DP)
- Uva 10201 Adventures in Moving - Part IV
- uva10201 - Adventures in Moving - Part IV
- UVA 10201 Adventures in Moving - Part IV(dp)
- uva_10201 - Adventures in Moving - Part IV (普通DP)
- UVA 10201 Adventures in Moving - Part IV
- UVA 10201 Adventures in Moving - Part IV 车站加油 dp
- UVA - 10201 Adventures in Moving - Part IV
- UVa 10201 Adventures in Moving - Part IV
- uva 10201 - Adventures in Moving - Part IV(dp)
- uva 10201 - Adventures in Moving - Part IV
- uva10201 - Adventures in Moving - Part IV(01背包)
- uva 10201 Adventures in Moving - Part IV
- UVA - 10201 Adventures in Moving - Part IV
- uva10201(Adventures in Moving - Part IV)
- 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(dp)