PAT甲级 1033
来源:互联网 发布:网络黑白txt百度云盘 编辑:程序博客网 时间:2024/05/18 22:16
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 86.00 12507.00 6007.00 1507.10 07.20 2007.50 4007.30 10006.85 300Sample Output 1:
749.17Sample Input 2:
50 1300 12 27.10 07.00 600Sample Output 2:
The maximum travel distance = 1200.00
#include <iostream>#include <math.h>#include <algorithm>#include <string.h>#include <vector>#include <stdio.h>#include <queue>using namespace std;#define MAXSIZE 500+10#define INF 0x7fffffffstruct node{ float cost; float dis;}station[MAXSIZE];int cmp(const void *a,const void *b){ node ta=*(node *)a,tb=*(node *)b; if(ta.dis==tb.dis){ return ta.cost-tb.cost; } return ta.dis-tb.dis;}int main(){ freopen("./in","r",stdin); float C,D,Davg,G; scanf("%f%f%f%f",&C,&D,&Davg,&G); int i,j,k; for(i=0;i<G;i++){ scanf("%f%f",&station[i].cost,&station[i].dis); } qsort(station,G,sizeof(node),cmp); if(station[0].dis!=0){ printf("The maximum travel distance = 0.00"); return 0; } int maxDis=C*Davg; int cur=0,pos=1; bool flag; float totalCost=0,totalDis=0; float minCost; int minCostIdx; float remain=0; while(true){ flag=false; minCost=INF; totalDis=station[cur].dis+remain*Davg; if(pos==G){ if((C-remain)*Davg+totalDis>=D){ totalCost+=station[cur].cost*(D-totalDis)/Davg; printf("%.2f",totalCost); }else{ printf("The maximum travel distance = %.2f",totalDis+(C-remain)*Davg); } break; } for(i=0;i<G-pos;i++){ if(maxDis<station[pos+i].dis-station[cur].dis)break; if(minCost>station[pos+i].cost){ minCost=station[pos+i].cost; minCostIdx=pos+i; } if(station[pos+i].cost<=station[cur].cost){ flag=true; break; } } if(minCost==INF){ printf("The maximum travel distance = %.2f",totalDis); return 0; } if(flag){ totalCost+=station[cur].cost*((station[pos+i].dis-station[cur].dis)/Davg-remain); remain=0; cur=pos+i; }else{ if(station[cur].dis+(C-remain)*Davg>=D){ totalCost+=(D-station[cur].dis)/Davg*station[cur].cost; printf("%.2f",totalCost); break; } totalCost+=(C-remain)*station[cur].cost; remain=C-(station[minCostIdx].dis-station[cur].dis)/Davg; cur=minCostIdx; } pos=cur+1; } return 0;}
- PAT(甲级)1033
- 浙大PAT甲级 1033
- PAT甲级1033
- PAT甲级 1033
- PAT 甲级
- PAT甲级 A1025.PAT RANKING
- PAT 甲级 1025 PAT Ranking
- PAT(甲级)1003
- PAT(甲级)1004
- PAT(甲级)1005
- PAT(甲级)1006
- PAT(甲级)1007
- PAT(甲级)1008
- PAT(甲级)1009
- PAT(甲级)1010
- PAT(甲级)1011
- PAT(甲级)1012
- PAT(甲级)1013
- C语言基础之你必须要知道的32个关键字
- Oralce12c新特性CDB与PDB
- 53. Maximum Subarray
- 关于springmvc+freemarker问题的整理
- Spring aop 一些问题
- PAT甲级 1033
- 《Android源码设计模式》之单例模式
- S3C6410核心初始化
- python_selenium(三)
- 第10章 Executor框架
- PAT甲级 1034 -- 没有注释
- PAT(Basic Level)_1002_写出这个数
- (专业写BUG系列)Foreach的只读性和List<T>的引用传递
- 获得随机数