Problem A: Adventures in Moving - Part IV

To help you move from Waterloo to the big city, you are considering renting a moving truck. Gas prices being so high these days, you want to know how much the gas for such a beast will set you back.

The truck consumes a full litre of gas for each kilometre it travels. It has a 200 litre gas tank. When you rent the truck in Waterloo, the tank is half full. When you return it in the big city, the tank must be at least half full, or you'll get gouged even more for gas by the rental company. You would like to spend as little as possible on gas, but you don't want to run out along the way.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

Input is all integers. The first integer is the distance in kilometres from Waterloo to the big city, at most 10000. Next comes a set of up to 100 gas station specifications, describing all the gas stations along your route, in non-decreasing order by distance. Each specification consists of the distance in kilometres of the gas station from Waterloo, and the price of a litre of gas at the gas station, in tenths of a cent, at most 2000.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

Output is the minimum amount of money that you can spend on gas to get you from Waterloo to the big city. If it is not possible to get from Waterloo to the big city subject to the constraints above, print "Impossible".

Sample Input

1

500
100 999
150 888
200 777
300 999
400 1009
450 1019
500 1399

Output for Sample Input

450550

题意:给定终点距离。和起点到终点之间的加油站位置和每个加油站每升油的价钱,求最少需要花的钱使得到终点后油剩100升。

思路:dp。i作为站点,j作为有多少升油。dp[i][j]表示在第i个站点有j升油所需要花的最少钱,k为在前一个站点加k升油。

状态转移方程dp[i][j] = min(dp[i][j], dp[i - 1][j + dis - k] + k * city[i].n)。

代码:

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include <algorithm>
using namespace std; int t, n, dp[105][205], num, i, j, k;
char sb[105];
struct City {
int d, n;
} city[105]; int min(int a, int b) {
return a < b ? a : b;
} int cmp(City a, City b) {
return a.d < b.d;
}
int main() {
scanf("%d", &t);
while (t --) {
num = 1;
scanf("%d%*c", &n);
while (gets(sb) != NULL && sb[0] != '\0') {
sscanf(sb, "%d%d", &city[num].d, &city[num].n);
num ++;
}
sort(city + 1, city + num, cmp);
for (i = 0; i <= num; i ++)
for (j = 0; j <= 200; j ++)
dp[i][j] = INT_MAX;
dp[0][100] = 0;
for (i = 1; i < num; i ++) {
int dis = city[i].d - city[i - 1].d;
for (j = 0; j <= 200; j ++) {
for (k = 0; k <= j; k ++) {
if (j + dis - k <= 200 && dp[i - 1][j + dis - k] != INT_MAX) {
dp[i][j] = min(dp[i][j], dp[i - 1][j + dis - k] + k * city[i].n);
}
}
}
}
if (dp[num - 1][100 + abs(n - city[num - 1].d)] != INT_MAX && 100 + abs(n - city[num - 1].d) <= 200) {
printf("%d\n", dp[num - 1][100 + abs(n - city[num - 1].d)]);
}
else
printf("Impossible\n");
if (t) printf("\n");
}
return 0;
}

UVA 10201 Adventures in Moving - Part IV(dp)的更多相关文章

  1. UVa 10201 Adventures in Moving - Part IV

    https://vjudge.net/problem/UVA-10201 题意: 给出到达终点的距离和每个加油站的距离和油费,初始油箱里有100升油,计算到达终点时油箱内剩100升油所需的最少花费. ...

  2. UVA 10201 DP

    Adventures in Moving - Part IV 题意: 汽车邮箱容量200升,最初有100升油,要求到达终点油箱中的油不少于100升的最小花费,不能到达终点输出Impossible. 汽 ...

  3. UVA.357 Let Me Count The Ways (DP 完全背包)

    UVA.357 Let Me Count The Ways (DP 完全背包) 题意分析 与UVA.UVA.674 Coin Change是一模一样的题.需要注意的是,此题的数据量较大,dp数组需要使 ...

  4. 训练指南 UVA - 10917(最短路Dijkstra + 基础DP)

    layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: tr ...

  5. UVA - 1025 A Spy in the Metro[DP DAG]

    UVA - 1025 A Spy in the Metro Secret agent Maria was sent to Algorithms City to carry out an especia ...

  6. UVa 1252 - Twenty Questions(状压DP)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. UVA 10891 Game of Sum(区间DP(记忆化搜索))

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  8. UVa 1220 - Party at Hali-Bula(树形DP)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. UVA 10529 - Dumb Bones(概率+区间dp)

    UVA 10529 - Dumb Bones option=com_onlinejudge&Itemid=8&category=518&page=show_problem&am ...

随机推荐

  1. MYSQLI - mysqli操作数据库

    <?php //模型类 class Model { //数据库连接 private $_conn = NULL; //where语句 private $_where = NULL; //表名称 ...

  2. <%@ include file=” ”%> ——最简洁易懂的解释

    <%@ include file=” ”%> 假如 在B.jsp 中,使用 <%@ include file=”A.jsp”%>  ,那么就是把 A.jsp 的内容 原封不动 ...

  3. 浙江大学2015年校赛B题 ZOJ 3861 Valid Pattern Lock

    这道题目是队友写的,貌似是用暴力枚举出来. 题意:给出一组数,要求这组数在解锁的界面可能的滑动序列. 思路:按照是否能够直接到达建图,如1可以直接到2,但是1不能直接到3,因为中间必须经过一个2. 要 ...

  4. 2014 HDU多校弟五场A题 【归并排序求逆序对】

    这题是2Y,第一次WA贡献给了没有long long 的答案QAQ 题意不难理解,解题方法不难. 先用归并排序求出原串中逆序对的个数然后拿来减去k即可,如果答案小于0,则取0 学习了归并排序求逆序对的 ...

  5. Python 内置函数 range的使用

    内置range函数可以用来方便的产生等差的数值序列.如: >>> range(5) [0, 1, 2, 3, 4] >>> range(1,5) [1, 2, 3, ...

  6. c语言‘\0’ ,‘0’, “0” ,0之间的区别

    首先比较一下‘\0’和‘0’的区别.有一个共同点就是它们都是字符,在c语言中,字符是按其所对应的ASCII码来存储的,一个字符占一个字节.请翻开你的ASCII字符集表吧,一般在你的C语言教材的附录上, ...

  7. 一起学习CMake – 02

    本节介绍如何用CMake来设置软件的版本号 在<一起学习CMake - 01>中我们看到了如何用CMakeLists.txt来构建一个最简单的工程,这一节里我们一起来看看如何用CMake对 ...

  8. DH11数字温湿度传感器

    DH11数字温湿度传感器是一种集温度.湿度一体的复合传感器,它能把温度和湿度物理量通过温.湿度敏感元件和相应电路转化成方便计算机.PLC.智能仪表等数据采集设备直接读取的数字量.DHT11由电阻式感湿 ...

  9. 第三届蓝桥杯 c/c++真题

    第三届蓝桥杯真题 c/c++ 以下题目我自己也并不是所有的题目都是一次性就能做对或是有结题思路的.有些题目也是经过查证网上相关的资料或是参考了别人的代码和解题思路才做出来的.总的来看,这份题目考了很多 ...

  10. MAMP:在 OSX 中搭建 Apache, MySQL, PHP 环境并本地安装、调试 WordPress

    MAMP 这个名字来源于 Macintosh Apache MySQL PHP,显然专门用来在 Mac 环境下搭建 Apache.MySQL.PHP 平台. 虽然 OSX 中已经预装了 Apache ...