您的位置:首页 > 其它

uva 10201 Adventures in Moving - Part IV

2016-07-07 01:21 489 查看
原题:

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

Sample Output

450550

题目大意:

给你一堆给你一辆车,油箱容量200起始位置给你100的油,让你走一定长度的距离,这个距离上有加油站,每个加油站给出位置没每升油的价格,现在问你到终点时油箱剩下100油时最少花多少钱。

(这题输入输出的方式反人类)

//参考博客http://blog.csdn.net/hcbbt/article/details/16369513

#include <bits/stdc++.h>
using namespace std;
const int inf=0x7fffffff;
int t,dis;
int dp[110][210];
int pri[210],pos[210];

//fstream in,out;
char ch[50];
int main()
{
//  ios::sync_with_stdio(false);
gets(ch);
sscanf(ch,"%d",&t);
gets(ch);
while(t--)
{
gets(ch);
sscanf(ch,"%d",&dis);
int inde=0;
while (gets(ch) != NULL && ch[0] != '\0') {
inde++;
sscanf(ch, "%d%d", &pos[inde], &pri[inde]);
if (pos[inde] > dis) inde--;
}
for(int i=0;i<=inde;i++)
{
for(int j=0;j<=200;j++)
dp[i][j]=inf;
}
dp[0][100]=0;
for(int i=1;i<=inde;i++)
{
int w=pos[i]-pos[i-1];
for(int j=0;j+w<=200;j++)
if(dp[i-1][j+w]!=inf)
dp[i][j]=dp[i-1][j+w];
for(int j=0;j<=200;j++)
{
for(int k=0;k<=j;k++)
if(j+w-k<=200&&dp[i-1][j-k+w]!=inf)
dp[i][j]=min(dp[i][j],dp[i-1][j+w-k]+pri[i]*k);
}
}
if (dis - pos[inde] > 100 || dp
[100 + dis - pos[inde]] == inf)
puts("Impossible");
else
printf("%d\n", dp[inde][100 + dis - pos[inde]]);
if (t) puts("");
}
return 0;
}


解答:

这题扔了很久,一直想自己做出来。今天花了不少时间去研究这道题,最后得出了这么个转移方程。

dp[i][j]=min(dp[i][j-k]+k*gas[i],dp[i-k][j+k],dp[i][j])其中状态量i为走的距离,j为油箱剩余油量

两个转移状态分别为在第i个加油站是否加油。时间复杂度是O(dis*gas*gas)其中dis是路线长度,gas是油箱的容量(200)。提交一次WA了,自己跑了30个数据,结果发现有部分答案错误。最后无奈,看了看别人的题解,发现状态量应该设置为加油站的个数和油箱容量,时间复杂度直接降下来了。转移方程

dp[i][j]=min(dp[i-1][j+pos[i]-pos[i-1]],dp[i-1][j-k+pos[i]-pos[i-1]+k*pri[i])

表示在第i个加油站是否加油,与当初的想法没什么区别。这题让我wa了将近10次,re了将近10多次。后来直接照着别人的输入输出和代码才交过=_=
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: