zoukankan      html  css  js  c++  java
  • 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

    题目大意:从起点到目的点有N个加油站,求从经过的站点最小的耗费油钱。
    解题思路:本着利用贪心算法来解,但代码一直敲不出!参考别人代码后,整理下。首先从点s1到s2,情况有几种。1、s1到s2在可达范围
    内(小于Cmax*Davg)时,如果s2的油价比s1的便宜,则在s1加的油量只需能够到s2就行;相反,如果s2的油价比s1的贵,则需要在s1加
    满油到s2,此时在s2时还有油剩余。在s2时加的油量只需Cmax-剩余的油量即可。2、如果s1到s2不可达(大于Cmax*Davg),此时只需在
    s1上加满油,任车子能走多远就走多远。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    #define INF 0x6ffffff
    int  Cmax,D,Davg,N;
    struct Station{
    	double price;
    	int dist;
    };
    Station sta[502];
    bool cmp(Station s1,Station s2){
    	return s1.dist<s2.dist;
    }
    int main(){
    	scanf("%d%d%d%d",&Cmax,&D,&Davg,&N);
    	int max_dist=Cmax*Davg;
    	int i,j;
    	for(i=0;i<N;i++){
    		scanf("%lf%d",&sta[i].price,&sta[i].dist);
    	}
    	sort(sta,sta+N,cmp);
    	if(sta[0].dist>0||N==0){
    		printf("The maximum travel distance = 0.00
    ");
    		return 0;
    	}
    	int targe;
    	double sum=0.0;
    	double left = 0.0;
    	i=0;
    	int t;
    	sta[N].dist=D;
    	sta[N].price=INF;
    	for(i=0;i<N;i++){
    		if(i!=0){
    			left-=((double)(sta[i].dist-sta[i-1].dist)/Davg);//算出从i-1到i的消耗油量。 
    		}
    		j=i+1;
    		while(j<N && sta[j].price>=sta[i].price){//查找比i站油价小的站点j。 
    			j++;
    		}
    		if(sta[j].dist-sta[i].dist>max_dist){
    			sum+=(double)(Cmax-left)*sta[i].price;
    			left=Cmax;
    		}else {
    			double d=(double(sta[j].dist-sta[i].dist))/Davg - left;
    			if(fabs(d)>1e-8&&d>0){
    				sum+=d*sta[i].price;
    				left = (double(sta[j].dist-sta[i].dist)/Davg);//因为j的油价更便宜,只要能够到J点后的油即可。 
    			}
    		}
    		if(sta[i+1].dist - sta[i].dist > max_dist){
    			printf("The maximum travel distance = %.2lf
    ",(double)(sta[i].dist+max_dist));
    			break;
    		}
    	} 
    	if(i == N){
    		printf("%.2lf
    ",sum);
    	}
    	return 0;
    }
    

      




          
  • 相关阅读:
    maven创建父子工程
    webservice之jersey简单实用
    EL表达式处理字符串
    oracle不等于1怎么查?
    day_07 搭建Tomcat服务器使用Servlet服务,后端接受前端请求过来的表单数据并使用
    Day_06 流程控制-循环结构-嵌套循环结构的原理解析
    Day05_流程控制02 循环结构
    day_5 流程控制 选择结构的两种常用语句的使用语法
    day_04 运算符详解
    day_03 变量的数据类型详解
  • 原文地址:https://www.cnblogs.com/grglym/p/7727265.html
Copyright © 2011-2022 走看看