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: 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<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;
}