九度1437 不知道哪错了,请求指点

公子LV 2017-03-21 10:07:07
/*

题目描述:

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.

输入:

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.

输出:

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.

样例输入:

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
50 1300 12 2
7.10 0
7.00 600

样例输出:

749.17
The maximum travel distance = 1200.00


*/

#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
struct gasta{
double p;//该加油站的油价
int dis;//该加油站距离杭州的距离
bool operator < (const gasta &A) const{
return dis<A.dis;
}
}sta[501];

int main(){
int Cmax,D,Davg,N;
while(scanf("%d%d%d%d",&Cmax,&D,&Davg,&N)!=EOF){
for(int i=0;i<N;i++){
scanf("%lf%d",&sta[i].p,&sta[i].dis);
}
sta[N].dis=D;//设置终点站
sta[N].p=0;
sort(sta,sta+N);
if(sta[0].dis>0){
printf("The maximum travel distance = 0.00\n");
continue;
}
int longd=Cmax*Davg;//加满油能行驶的距离
int leftD=0;//该车还能行驶的距离
double sum=0;//总共花去多少钱
bool flag=true;//能否到达
for(int i=0;i<N;i++){//达到当前加油站
if(i==0){
leftD=0;
}else{
leftD-=sta[i].dis-sta[i-1].dis;
}
if(longd+sta[i].dis<sta[i+1].dis){//判断从当前站能否到达下一站
double X=1.0*sta[i].dis+longd;
printf("The maximum travel distance = %.2lf\n",X);
flag=false;
break;
}
int min=i;//油价最低的站的编号
if(leftD<sta[i+1].dis-sta[i].dis){//决定在该站是否需要加油
for(int j=i;j<=N;j++){//决定在该站需要加多少油
if(sta[j].dis<longd+sta[i].dis && sta[min].p>sta[j].p){
min=j;
break;
}
}
if(min==i){//加满
sum+=sta[i].p*(Cmax-1.0*leftD/Davg);
leftD=longd;
}else{//只需加油到下一个加油站的量为止
double tmp=sta[min].dis-sta[i].dis-leftD*1.0;
if (fabs(tmp)>1e-5 && tmp>0){//这么做是为了防止可能出现的负数
sum+=tmp/Davg*sta[i].p;
leftD=sta[min].dis-sta[i].dis;
}
}
}
}
if(flag==true){
printf("%.2lf\n",sum);
}
}
return 0;
}

谢谢!
...全文
137 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧