Adventures in Moving - Part IV - UVa 10201 dp

本文探讨了如何在从Waterloo到大城市的旅途中,通过合理选择加油站,以最小的成本消耗汽油。利用动态规划算法,计算出在满足特定条件下的最低费用方案。

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升,每到一个加油站可以加任意多的汽油,最多到200升的容量,到达目的地后车里至少要剩100升的汽油,问最少的花费是多少。

思路:dp[i][j]表示到第i个加油站时还剩j升汽油的最少花费。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int dp[10010][210],dis[110],pri[110],n,INF=100000000;
char c;
void get()
{ while(true)
  { c=cin.peek();
    if(c=='\n')
     break;
    n++;
    scanf("%d%d",&dis[n],&pri[n]);
    getchar();
   }
}
int main()
{ int T,t,i,j,k,len,ans;
  scanf("%d",&T);
  for(t=1;t<=T;t++)
  { scanf("%d",&len);
    getchar();
    n=0;
    dis[0]=0;
    if(t!=T)
     get();
    else
    { n=1;
      while(~scanf("%d%d",&dis[n],&pri[n]))
       n++;
      n--;
    }
    for(i=0;i<=len;i++)
     for(j=0;j<=200;j++)
      dp[i][j]=INF;
    dp[0][100]=0;
    for(i=1;i<=n;i++)
    { for(j=dis[i]-dis[i-1];j<=200;j++)
      { if(dp[i-1][j]<INF)
        { k=j-dis[i]+dis[i-1];
          dp[i][k]=min(dp[i][k],dp[i-1][j]);
          k++;
          while(k<=200)
          {dp[i][k]=min(dp[i][k],dp[i][k-1]+pri[i]);
           k++;
          }
        }
      }
    }
    ans=INF;
    for(i=100+len-dis[n];i<=200;i++)
     ans=min(ans,dp[n][i]);
    if(t!=1)
     printf("\n");
    if(ans>=INF)
     printf("Impossible\n");
    else
     printf("%d\n",ans);
  }
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值