标签:dp
状态转移方程:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#define N 2005
#define M 205
using 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;
}
uva 10201 Adventures in Moving - Part IV (DP)
标签:dp
原文地址:http://blog.csdn.net/llx523113241/article/details/45342447