zoukankan      html  css  js  c++  java
  • 九度 题目1437:To Fill or Not to Fill

    题目描述:

    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<iostream>
    #include<algorithm>
    #include<cstdio>
    using namespace std;
    typedef struct
    {
    	double p;
    	double d;
    }station;
    bool compare(station s1,station s2)
    {
    	return s1.d<s2.d;
    }
    int main()
    {
    	int n,i,j,k;
    	double cmax,d,davg,c,min,price,dis;
    	while(cin>>cmax>>d>>davg>>n)
    	{
    		station s[501];
    		for(i=0;i<n;i++)
    			cin>>s[i].p>>s[i].d;
    		sort(s,s+n,compare);
    		s[n].d=d;
    		s[n].p=0;
    		if(s[0].d!=0)
    		{
    			cout<<"The maximum travel distance = 0.00"<<endl;
    			continue;
    		}
    		c=cmax;//油箱剩余容量
    		price=dis=0;
    		for(i=0;i<n;)
    		{
    			if(s[i+1].d-s[i].d>cmax*davg)
    			{
    				dis+=cmax*davg;
    				break;
    			}
    			k=-1;
    			for(j=i+1;j<n&&(s[j].d-s[i].d)<=davg*cmax;j++)//找能到达的比现在的便宜的最近的加油站
    				if(s[j].p<s[i].p)
    				{
    					k=j;
    					break;
    				}
    			if(k==-1)//能到的都比现在的贵
    			{
    				if(cmax*davg>=(d-s[i].d))//现在的装满油能到终点站
    				{
    					dis=d;
    					price+=(d-s[i].d-(cmax-c)*davg)/davg*s[i].p;
    					break;
    				}
    				else//找一个能到达的最便宜的
    				{
    					min=s[i+1].p;
    					k=i+1;
    					for(j=i+2;j<n&&(s[j].d-s[i].d)<=davg*cmax;j++)
    					{
    						if(s[j].p<min)
    						{
    							min=s[j].p;
    							k=j;
    						}
    					}
    					dis=s[k].d;
    					price+=c*s[i].p;
    					c=(s[k].d-s[i].d)/davg;
    					i=k;
    				}
    			}
    			else
    			{
    				dis=s[k].d;
    				price+=(s[k].d-s[i].d-(cmax-c)*davg)/davg*s[i].p;
    				c=cmax;
    				i=k;
    			}
    		}
    		if(dis==d)
    			printf("%.2lf
    ",price);
    		else
    			printf("The maximum travel distance = %.2lf
    ",dis);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    Yii CGridView 之 SQL 语句
    Yii的srbac拓展中“用户已经获授权项”无法查看
    MyBatis通过注解实现映射中的嵌套语句和嵌套结果
    MyBatis一对多映射简单查询案例(嵌套结果)
    MyBatis绑定Mapper接口参数到Mapper映射文件sql语句参数
    将前端请求中的数据绑定到Spring MVC响应方法中参数的四种方法
    MyBatis一对多映射简单查询案例(嵌套Mapper映射文件中的sql语句)
    Spring MVC与html页面的交互(以传递json数据为例)
    使用Spring JDBC连接数据库(以SQL Server为例)
    spring mvc访问html页面404报错解决
  • 原文地址:https://www.cnblogs.com/argenbarbie/p/3178648.html
Copyright © 2011-2022 走看看