pat甲级习题1033

本文探讨了一种使用深度优先搜索算法来寻找从杭州到其他城市最经济的驾驶路线的方法,考虑到加油站的不同价格和有限的油箱容量。通过分析输入数据,包括油箱最大容量、目的地距离、平均油耗和沿途加油站信息,算法旨在找到最低成本的加油策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1033 To Fill or Not Fill

我感觉自己算法应该是对的,不知道为啥算出来结果不对,可能是浮点数计算产生误差了吗
这道题我使用深度优先搜索算法做的,仅供参考,不是正确答案。如果有大佬看出来我代码问题出在哪里,劳烦指正感激不尽
1033 To Fill or Not to Fill (25 分)

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

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
int n, d, cmax, davg;
int TtlFuel;
int MaxTravelDistance = 0;
struct station{
    double pi=0.0;
    int di=0;
}arr[505];
bool mycmp1(station a,station b){
    return a.di < b.di;
}
int Distance[505];
int fuel[505];
vector<double> ans;
void DFS(int i,int omitgas,double cost){//i为第几个加油站,omitgas为邮箱剩余油量,cost为花了多少钱
    if(i==n+1){//设置递归终点
        if(omitgas!=0){
            return;
        }
        ans.push_back(cost);
        return;
    }
    int addtion = cmax - omitgas;//要加多少油能加满
    DFS(i + 1, cmax-Distance[i+1]/davg, cost + addtion * arr[i].pi);
    if((Distance[i + 1] - omitgas * davg)<=0){
        DFS(i + 1, omitgas-Distance[i+1]/davg, cost);//油量足够跑到下一站,下一站再说
    }
    else{//油量不够,加到足够跑到下一站的油量
        int lossDistance = Distance[i + 1] - omitgas*davg;
        DFS(i + 1, 0, cost+arr[i].pi*lossDistance / davg);//到地方跑空了油箱,花了加到油够跑的钱
    }
}
int main(){
    while(cin>>cmax>>d>>davg>>n){
        ans.clear();
        if(d%davg){
            TtlFuel = d / davg + 1;
        }
        else{
            TtlFuel = d / davg;
        }
        MaxTravelDistance = davg * cmax;
        if(MaxTravelDistance>=d){
            cout << "The maximum travel distance = " << MaxTravelDistance<<endl;
            continue;
        }
        for (int i = 0; i < n;i++){
            Distance[i] = 0;
            fuel[i] = 0;
        }
        for (int i = 1; i <= n; i++)
        {
            cin >> arr[i].pi >> arr[i].di;
        }
        sort(arr+1, arr + n+1,mycmp1);
        int cnt = 0;
        for (int i = 1; i <= n; i++){
            int t = arr[i - 1].di;
            Distance[i] = arr[i].di - t;
            cnt += Distance[i];
        }
        Distance[n + 1] = d - cnt;
        DFS(1, 0, 0);
        sort(ans.begin(), ans.end());
        printf("%.02f", ans[0]);
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值