1. 程式人生 > >1033 To Fill or Not to Fill @PTA

1033 To Fill or Not to Fill @PTA

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: C​max​​ (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D​avg​​ (≤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: P​i​​, the unit gas price, and D​i​​ (≤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

很好的貪心題目。

分情況做對策:

設在一個車站加滿油後能跑出的最大距離為max_dis

同時可以把終點看作一個車站,價格設為0,方便處理。

一、 max_dis範圍內沒有車站,也沒有到達終點,GG,輸出該車站的距離+max_dis;

二、範圍內有車站:

1.某一車站價格比當前車站小,那麼加夠恰好到達該車站的油開往該車站。

2.範圍內所有車站價格都比當前車站大,加滿油箱,直接開往能夠到達的最遠的車站,因為當前車站加的油是最省錢的。注意,這種情況下到達最遠車站時油箱裡的油可能還有剩餘,也就是說從每個車站出發時還要考慮到油箱是否還有油。

3.到達終點站。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <set>
#include <stack>
#include <iomanip>
#include <cstdlib>
#include <queue>
#define MAXN 100003

using namespace std;

struct Node{
	double p;
	double di;
}sta[503];

bool cmp(const Node &a,const Node &b){
	return a.di < b.di;
}

int main(){
	double c,d,da;
    int n;
	scanf("%lf%lf%lf%d",&c,&d,&da,&n);
 	for(int i=0;i<n;++i){
		scanf("%lf%lf",&sta[i].p,&sta[i].di);
	}
	sort(sta,sta+n,cmp);
    sta[n].p = 0;
    sta[n].di = d; //設定目的地為一個車站,價格為0
	double max_dis = da*c; //油箱加滿時能夠丟擲的最遠距離
	int cur = 0; //車子剛從哪個車站駛出或正位於哪個車站
	double dis = 0.0; //車子已經開出的距離,如果dis=某個車站的距離,說明車子正位於該車站;否則,說明車子剛從前面某個車站駛出,這樣就可以表示利用油箱中剩餘油能夠跑到的最遠距離
	double cost = 0.0; //車子開到當前距離的最小花費
    if(sta[0].di != 0.0){ //特殊情況,起點沒有車站時,車子開不出來
        printf("The maximum travel distance = 0.00\n");
        return 0;
    }
    while(true){
        if(cur == n){ //車子到達終點
            printf("%.2lf\n",cost);
            return 0;
        }
        int pos = -1;//當前方車站價格都大於當前車站時,記錄能夠到達的最遠的車站
        for(int i=cur+1;i<=n;++i){
            if(sta[cur].di + max_dis >= sta[i].di){
                if(sta[i].p < sta[cur].p){ //當某一車站價格更小時
                    cost += sta[cur].p * (sta[i].di - dis)/da;
                    dis = sta[i].di;
                    cur = i;
                    break;
                }
                else{
                    pos = i;
                }
            }
            else {
                if(pos == -1){ //說明前方max_dis範圍內沒有車站
                    printf("The maximum travel distance = %.2lf\n",sta[cur].di + max_dis);
                    return 0;
                }
                else{ //否則,滿油開到能到達的最遠的車站,注意,車子不一定就停靠在該車站,也有可能駛過該車站,但要在經過該車站時維護狀態。
                    double tmp = dis;
                    dis = sta[cur].di + max_dis;
                    cost += sta[cur].p * (dis-tmp)/da;
                    cur = pos;
                    break;
                }
            }
        }
    }
    // printf("%.2lf\n",cost += sta[cur].p*(d-dis)/da);
	return 0;
}