To Fill or Not to Fill

题目描述:

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.

输入:

For each case, the first line contains 4 positive numbers: Cmax (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=20), the average distance per unit gas that the car can run; and N (<= 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (<=D), the distance between this station and Hangzhou, for i=1,...N. All the numbers in a line are separated by a space.

输出:

For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print "The maximum travel distance = X" where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

样例输入:
50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300
50 1300 12 2
7.10 0
7.00 600
样例输出:
749.17
The maximum travel distance = 1200.00
来源:

2012年浙江大学计算机及软件工程研究生机试真题

一种思路:

http://blog.csdn.net/j597960549/article/details/20561829

我的思路:

将终点也作为一个加油站。记录当前距离与油量。

找下一个比当前(A)便宜的站点B。如果当前油量可以到达,则不用加油,等到了B再做决定。如果无法到达,则加油量到刚好达到B,然后

行驶至A+1(如果无法行驶至A+1站,则不可达),再做决定。

PAT上AC,九度WA,先不管了

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
	float pice;
	float dis;
};
int cmp(const void*x1, const void * x2)
{
	node n1 = *(node*)x1;
	node n2 = *(node*)x2;
	return n1.dis - n2.dis;
}
node in[500 + 3];
int main()
{
	freopen("E:\\in.txt", "r", stdin);
	int i, j, k;
	float tank, dis, per;
	int n;
	while (scanf("%f%f%f%d", &tank, &dis, &per, &n) != EOF)
	{
		float cost = 0;
		int flag = 0;
		for (i = 0; i < n; i++)
		{
			scanf("%f%f", &in[i].pice, &in[i].dis);
			if (in[i].dis == 0)
				flag = 1;
		}
		if (flag == 0)
		{
			printf("The maximum travel distance = 0.00\n");
			continue;
		}
		in[i].pice = -1;
		in[i].dis = dis;
		qsort(in, n + 1, sizeof(node), cmp);
		int jie = 0;
		for (i = 0; i < n + 1; i++)
		{
			if (in[i].pice == -1)
				break;
		}
		jie = i;
		int cu = 0;
		float ge = 0;
		cost = 0;
		float yu = 0;
		while (cu < jie)
		{
			int next = cu;
			ge = yu*per;
			while (in[next].pice >= in[cu].pice)
			{
				next++;
			}
			if ((in[next].dis - in[cu].dis) <= ge)
			{

				yu -= (in[next].dis - in[cu].dis) / float(per);
				cu = next;
			}
			else
			{
				float temp = (in[next].dis - in[cu].dis) - ge;
				float jia = temp / float(per);
				float yutemp = yu;
				yu += jia;
				if (yu > tank)
					yu = tank;
				cost += (yu - yutemp)*in[cu].pice;
				yu -= (in[cu + 1].dis - in[cu].dis) / float(per);
				if (yu < 0)
					break;
				cu++;
			}

		}
		if (jie == cu)
			printf("%.2f\n", cost);
		else
			printf("The maximum travel distance = %.2f\n", tank*per + in[cu].dis);
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值