uva 10201 Adventures in Moving - Part IV (DP)

uva 10201 Adventures in Moving - Part IV

题目大意:借了一辆车,车里有100单位的油。要到达N米外的目的地(每走一米消耗一个单位的油),在这一段路程中,有若干个加油站,给出的数据是每个加油站的位置和加一单位油的价格。要求到达目的地且剩下100单位油的最小消费。(到达不了则输出Impossible)

解题思路:dp[i][j]数组代表的是第i个加油站油量为j的最小费用。

状态转移方程:

dp[i][j]=min(dp[i][j],dp[i?1][j+(mile[i]?mile[i?1])])

dp[i][j]=min(dp[i][j],min(dp[i][k]+(j?k)?money[i]))

#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;
}
时间: 2024-08-04 08:44:40

uva 10201 Adventures in Moving - Part IV (DP)的相关文章

UVa 10201 Adventures in Moving - Part IV

https://vjudge.net/problem/UVA-10201 题意: 给出到达终点的距离和每个加油站的距离和油费,初始油箱里有100升油,计算到达终点时油箱内剩100升油所需的最少花费. 思路: 我们用d[i][j]来表示车子在第 i 个加油站时还剩 j 升油量的最小花费. 先说一下转移方程吧,d[i][j] = min(d[i][j], d[i - 1][j + l - k] + k*b[i]),k代表的是在 i 这个加油站所加的油量,加了之后的总油量就是 j . 需要注意的是,油

uva10201 - Adventures in Moving - Part IV(01背包)

题目:uva10201 - Adventures in Moving - Part IV(01背包) 题目大意:一辆车要走D距离,然后它有个200L油箱,并且一开始有100L,现在给你一路上你会遇到的加油站,和这个加油站每升油的价钱,要求你最后到终点的时候油需要大于等于100L,问你加油最少的费用.如果到达不了目标地点就输出Impossible. 解题思路:首先要先到达这个加油站,然后就相当这个加油站你选不选择加油,选择加油了那么又要加多少油.dp[j][i]代表到达第i个加油站还有jL油,dp

uva 357 Let Me Count The Ways (DP)

uva 357 Let Me Count The Ways After making a purchase at a large department store, Mel's change was 17 cents. He received 1 dime, 1 nickel, and 2 pennies. Later that day, he was shopping at a convenience store. Again his change was 17 cents. This tim

【UVA - 10815】Andy&#39;s First Dictionary (set)

Andy's First Dictionary Description 不提英文了 直接上中文大意吧 XY学长刚刚立下了再不过CET就直播xx的flag,为了不真的开启直播模式,XY学长决定好好学习英语.于是他每天都读一篇只包含生词的英语文章,并以自己高达450的智商在一秒钟之内记忆下来. 现在给你一篇XY学长今天要读的文章,请你写一个程序,输出他都学习到了哪些单词.要求:如果文章中有相同的单词,那么仅仅输出一次:而且如果两个单词只有大小写不同,将他们视为相同的单词. Input 测试数据将输入

【UVA】10285-Longest Run on a Snowboard(动态规划)

这题出简单了,不需要打印路径. 状态方程dp[i][j] = max(dp[i-1][j],dp[i][j-1],dp[i+1][j],dp[i][j+1]); 14003395 10285 Longest Run on a Snowboard Accepted C++ 0.026 2014-08-07 11:43:51 枚举每个点进行遍历,使用记忆化搜索.要不会超时. #include<cstdio> #include<cstring> #include<iostream&

UVA 1025 A Spy in the Metro(DP)

Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After several thrilling events we find her in the first station of Algorithms City Metro, examining the time table. The Algorithms City Metro consists of a s

UVA 580 - Critical Mass(DP)

题目链接:580 - Critical Mass 题意:一个栈,里面可以放L和U,有三个连续的U就是不安全的,问共有几种不安全的情况 思路:dp,dp[i][j][k],表示放到第i个,最后两个状态为j,k表示有没有出现不安全.然后去记忆化搜索一下就可以了 然后还有一种做法是,先考虑安全的情况,在用总情况(1<<n 种)减去安全的情况,安全的情况的递推方式很容易dp[i]表示放第i个, dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3]. 不过这题都没给数据范围

uva 10635 Prince and Princess(DP)

uva 10635 Prince and Princess(DP) In an n x n chessboard, Prince and Princess plays a game. The squares in the chessboard are numbered 1, 2, 3 ... n*n, as shown below: Prince stands in square 1, make p jumps and finally reach square n*n. He enters a

UVA 1371 - Period(DP)

6.4 一些说明 数据属性可以重写同名的方法属性.这是为了避免在大型系统中产生问题的意外名称冲突.所以用一些减少冲突的常用方法是很有效果的.常用的方法包括:大写字母方法名称,用唯一的字符串来做为数据属性的名称(可以是个下划线_)或者用动词命名方法和用名字命名数据属性. 数据属性就像和对象的普通用户一样可以被方法引用.换句话说,类不能用来实现纯净的数据类型.事实上,在python中不能强制数据隐藏,一切基于约定.(另一方面,如C中写的,python的实现可以做到完全隐藏实现细节并且在必要是可以控制