uva10201 - Adventures in Moving - Part IV

Problem A: Adventures in Moving - Part IV

To help you move from Waterloo to the big city, you are consideringrenting a moving truck. Gas prices being so high these days, you want toknow 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. Ithas a 200 litre gas tank. When you rent the truck in Waterloo, the tankis half full. When you return it in the big city, the tank must be atleast half full, or you'll get gouged even more for gas by the rentalcompany. You would like to spend as little as possible on gas, but youdon'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 kilometresfrom Waterloo to the big city, at most 10000. Next comes a set of up to100 gas station specifications, describing all the gas stations alongyour route, in non-decreasing order by distance. Each specification consists of the distance in kilometres ofthe gas station from Waterloo, and the price of a litre of gas at thegas 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 getyou from Waterloo to the big city. If it is not possible to get fromWaterloo 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

 

  车要从起点到终点,油气罐容量200,一开始有100的油,升序给出各个加油站离起点的位置和这个加油站加每单位油的价格,到终点的油至少要100,问最少花多少钱。

  开始想的是dp[i][j]是距离为i油为j的最少钱,其实这样就有很多不必要的计算,在各个地方的最优值就是它最近的上一个加油站的最优值加上之间的距离(如果可能),所以只要用dp[i][j]表示第i个加油站油量为j的最优值。状态转移方程有点难想。

 

#include<cstring>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
int a[110],b[110];
int dp[10010][210];
int main(){
   freopen("in.txt","r",stdin);
    int T,N,K;
    scanf("%d",&T);
    char s[100];
    while(T--){
        scanf("%d",&N);
        getchar();
        K=1;
        int i,j,k;
        while(gets(s)!=NULL&&s[0]!=0){
            sscanf(s,"%d%d",&a[K],&b[K]);
            K++;
        }
        memset(dp,INF,sizeof(dp));
        a[0]=0;
        dp[0][100]=0;
        for(i=1;i<K;i++)
        for(j=0;j<=200;j++){
            if(j+a[i]-a[i-1]<=200) dp[i][j]=dp[i-1][j+a[i]-a[i-1]];
            for(k=0;k<j;k++) dp[i][j]=min(dp[i][j],dp[i][k]+(j-k)*b[i]);
        }
        if(dp[K-1][100+N-a[K-1]]!=INF) printf("%d\n",dp[K-1][100+N-a[K-1]]);
        else printf("Impossible\n");
        if(T) puts("");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值