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

      

  • 相关阅读:
    HTML5数据推送SSE原理及应用开发
    用Docker构建分布式Redis集群
    开发者必备的12个JavaScript库
    分享:我用一天时间开发的 新年送祝福 微信手机网站(可在线体验附图)(要代码的留下邮箱)
    祝福csdn回望2014,展望2015 大致可以这样总结和展望
    对 云寻觅贴吧(http://tieba.yunxunmi.com/)的简要分析
    开源前夕先给大家欣赏一下我用C语言开发的云贴吧 网站自动兼容-移动、手机、PC自动兼容云贴吧
    舞蹈模特欣欣(六)棚拍私房 大家看看像小龙女(李若彤)吗?
    终于解决了贴吧手机版的一个重大BUG
    比基尼美女_人像摄影吧主题
  • 原文地址:https://www.cnblogs.com/argenbarbie/p/3178648.html
Copyright © 2011-2022 走看看