这个题目,感觉描述不完整,不多说了,勉强Ac
状态: dp[i][j] 表示在第i个加油站中还有j升油的最小花费
状态转移: dp[i][j] = min(dp[i-1][k+dist[i-1,i]], dp[i][k-x]+cost(x))
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define PRICE 0
#define DISTANCE 1
#define STATUS 2
#define MAX_PORT 256
#define MAX_GAS 201
#define INF 0x3f3f3f3f
char str[MAX_PORT];
int port[MAX_PORT][STATUS], dp[MAX_PORT][MAX_GAS];
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int n, d, x, y, idx, rst, flag(0);
scanf("%d", &n);
for(; n; n --) {
if( (flag ++) ) {
printf("\n");
}
scanf("%d", &d); getchar(); idx = 1;
while( NULL != gets(str) ) {
if( '\0' == *str ) {
break;
}
sscanf(str, "%d %d", &x, &y);
port[idx][DISTANCE] = x; port[idx ++][PRICE] = y;
}
memset(dp, 0x3f, sizeof(dp));
if( port[1][DISTANCE] > (MAX_GAS>>1) ) {
printf("Impossible\n"); continue;
}
rst = (MAX_GAS>>1)-port[1][DISTANCE]; dp[1][rst] = 0;
for(int i = rst; i < MAX_GAS; i ++) {
dp[1][i] = port[1][PRICE]*(i-rst);
}
for(int i = 2; i < idx; i ++) {
for(int j = 0; j < MAX_GAS; j ++) {
rst = j+(port[i][DISTANCE]-port[i-1][DISTANCE]);
dp[i][j] = (rst < MAX_GAS)? dp[i-1][rst] : INF;
for(int k = 0; k <= j; k ++) {
dp[i][j] = min(dp[i][j], dp[i][k]+port[i][PRICE]*(j-k));
}
}
}
if( d-port[idx-1][DISTANCE] > (MAX_GAS>>1) || INF <= dp[idx-1][ (MAX_GAS>>1)+d-port[idx-1][DISTANCE] ] ) {
printf("Impossible\n"); continue;
}
printf("%d\n", dp[idx-1][ (MAX_GAS>>1)+d-port[idx-1][DISTANCE] ]);
}
return 0;
}
uva_10201 - Adventures in Moving - Part IV (普通DP)
最新推荐文章于 2016-07-07 01:21:49 发布