居然用例2的起点压根没有加油站。。。。MMP
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.
Input Specification:
Each input file contains one test case. 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.
Output Specification:
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.
Sample Input 1:
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
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 2
7.10 0
7.00 600
Sample Output 2:
The maximum travel distance = 1200.00
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAXCAP 100
#define MAXDIST 30000
#define MAXTRAVEL 20
#define MAXSTATION 500
struct gasStation
{
float price; /**< 油站油价 */
int dist; /**< 油站距离杭州距离 */
};
typedef struct gasStation STATION;
int myCmp(const void *a, const void* b)
{
STATION x = *(STATION *)a;
STATION y = *(STATION *)b;
return x.dist - y.dist;
}
void testPrint(STATION a[], int num)
{
int i;
for(i=0; i<num; i++)
{
printf("%d %d %0.2f\n", i, a[i].dist, a[i].price);
}
}
int main()
{
freopen("D:\\OneDrive\\CS\\PAT\\test.txt", "r", stdin);
/**< 数据输入部分 */
float cap;
int distance, tvlSpd, numOfSta;
scanf("%f %d %d %d", &cap, &distance, &tvlSpd, &numOfSta);
STATION *staList = (STATION *)malloc(numOfSta*sizeof(STATION));
//STATION staList[numOfSta];
int i;
for(i=0; i<numOfSta; i++)
{
scanf("%f %d", &staList[i].price, &staList[i].dist);
//printf("%d %d %f\n", i, staList[i].dist, staList[i].price);
}
/**< 数据输入结束 */
qsort(staList, numOfSta, sizeof(staList[0]), myCmp);
//testPrint(staList, numOfSta);
float tank = 0.0; /**< 当前油箱油量 */
int curDist = 0; /**< 当前走了多远 */
int curSta = 0; /**< 当前在哪个油站 */
int nextSta = 0; /**< 下个油站应该是哪个 */
float needGas = 0; /**< 每次加油需要的油量 */
float pay = 0; /**< 当前用了多少钱 */
int maxRange = tvlSpd*cap; /**< 加满油最大续航 */
while(curDist<distance || curSta<numOfSta) /**< 当当前距离大于目的地距离或者没有加油站了,跳出循环(其实这条判断并没有太大作用) */
{
/**< 如果maxRange内有加油站 */
bool cheaper = false; /**< 是否存在比当前加油站更便宜的油站 */
int cheapest = curSta+1;
for(nextSta = curSta+1; nextSta<numOfSta && staList[nextSta].dist - staList[curSta].dist <= maxRange; nextSta++)
{
if(staList[nextSta].price <= staList[curSta].price)
{/**< 找到有比当前油价更便宜的油站,直接break */
cheaper = true;
cheapest = nextSta;
break;
}
if(staList[nextSta].price < staList[cheapest].price)
{/**< 没有更便宜就找最便宜的那家 */
cheapest = nextSta;
}
}
nextSta = cheapest;
//printf("\n%d -> %d\n", curSta, nextSta);
/**< 如果maxRange范围内有目的地 且目的地范围内没有更便宜的加油站,所以就在当前加油站加油至足够到达目的地 */
if(staList[curSta].dist+maxRange >= distance && !cheaper)
{
needGas = (float)(distance - staList[curSta].dist)/tvlSpd - tank;
pay += needGas * staList[curSta].price;
//printf("OVER needGas:%0.2f, pay:%0.2f\n", needGas, needGas * staList[curSta].price);
curDist = distance;
break;
}
/**< 如果maxRange范围内已经没有加油站 */
if(curSta+1 == numOfSta || staList[curSta+1].dist - staList[curSta].dist > maxRange)
{
curDist += maxRange;
//printf("curDist:%d\n", curDist);
needGas = cap - tank;
pay += needGas * staList[curSta].price;
break;
}
if(staList[nextSta].price <= staList[curSta].price)
{/**< 下一个加油站更便宜,所以在当前加油站只要加到足够去下一个加油站就可以了 */
needGas = (float)(staList[nextSta].dist - staList[curSta].dist)/tvlSpd - tank;
// printf("from %d to %d, travel dist:%d\n", staList[curSta].dist, staList[nextSta].dist, (staList[nextSta].dist - staList[curSta].dist));
// printf("need:%0.2f\n", needGas);
pay += needGas*staList[curSta].price;
curSta = nextSta;
curDist = staList[curSta].dist;
tank = 0;
}
else
{/**< 没有更便宜的加油站,就在当前加油站加满! */
needGas = cap - tank;
pay += needGas*staList[curSta].price;
// printf("from %d to %d, travel dist:%d, pay:%0.2f\n", staList[curSta].dist, staList[nextSta].dist, (staList[nextSta].dist - staList[curSta].dist), needGas*staList[curSta].price);
// printf("need:%0.2f - %0.2f = %0.2f\n", cap, tank, needGas);
tank = cap - (float)(staList[nextSta].dist - staList[curSta].dist)/tvlSpd;
curSta = nextSta;
curDist = staList[curSta].dist;
}
//printf("dist:%d, pay:%0.2f\n",curDist, pay);
}
if(staList[0].dist!=0) /**< 居然有起点没有加油站的情况 */
{
printf("The maximum travel distance = 0.00");
}
else if(curDist<distance)
{
printf("The maximum travel distance = %0.2f", (float)curDist);
}
else
{
printf("%.2f", pay);
}
system("pause");
}