uva10201 - Adventures in Moving - Part IV

来源:互联网 发布:怎么取消淘宝客推广 编辑:程序博客网 时间:2024/05/06 20:27

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

1500100 999150 888200 777300 999400 1009450 1019500 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 0x3f3f3f3fusing 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;}