PAT 1033. To Fill or Not to Fill (25)


1033. To Fill or Not to Fill (25)
時間の制限
10 ms
メモリ制限
32000 kB
コード長制限
16000 B
クイズルーチン
Standard
作成者
ZHANG, Guochuan
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 <cstdio>
#include <cstdlib>
#include <climits>


typedef struct{
	double price;
	double dist;
}STATION;

int cmp(const void *a,const void *b){
	int ret;
	STATION *pa = (STATION *)a;
	STATION *pb = (STATION *)b;
	return pa->dist-pb->dist;
}

STATION st[501];

int main(){
	double cmax,dist,davg,currgas,res,minprice;
	int n,i,j,pos;

	scanf("%lf %lf %lf %d",&cmax,&dist,&davg,&n);
	
	for(i=0;i<n;++i){
		scanf("%lf %lf",&st[i].price,&st[i].dist);
	}

	st[n].price = 0;
	st[n].dist = dist;

	qsort(st,n,sizeof(STATION),cmp);


	if(st[0].dist>0){
		printf("The maximum travel distance = 0.00
"); return 0; } currgas = 0.0; double limit = cmax * davg; res = 0.0; for(i=0;i<n;){ if(st[i+1].dist-st[i].dist>limit){//can't reach the destination printf("The maximum travel distance = %.2lf
",st[i].dist + limit); return 0; } pos = i; minprice = st[i].price; // , , for(j=i+1;j<=n && st[j].dist-st[i].dist<=currgas*davg;++j){ if(st[j].price<minprice){ minprice = st[j].price; pos = j; } } if(pos!=i){// , ( ), , , currgas -= ((st[pos].dist-st[i].dist)/davg); i = pos; continue; } // // , // , for(j=i+1;j<=n && st[j].dist-st[i].dist<=limit;++j){ if(st[j].price<minprice){ minprice = st[j].price; pos = j; break; } } // : ( ), if(pos!=i){ res += ((st[pos].dist-st[i].dist)/davg-currgas)*st[i].price; currgas = 0;// currgas , i = pos; continue; } minprice = INT_MAX; // , // , for(j=i+1;j<=n && st[j].dist-st[i].dist<=limit;++j){ if(st[j].price<minprice){ minprice = st[j].price; pos = j; } } res += (cmax-currgas)*st[i].price; currgas = cmax-(st[pos].dist-st[i].dist)/davg; i = pos; } printf("%.2lf
",res); return 0; }