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

      

  • 相关阅读:
    ADO.NET FOR MySQL帮助类
    遍历文件夹及其子文件夹下的.pdf文件,并解压文件夹下所有的压缩包
    history.back();谷歌浏览器,iframe后退问题
    Request.url请求属性
    正则表达式 取两字符中间的字符串(双向非贪婪模式)
    MVC 项目中为什么会有两个web.config
    c#动态编译并动态生成dll
    siteserver cms选择栏目搜索无效
    jquery修改Switchery复选框的状态
    ntko office在线编辑控件问题记录
  • 原文地址:https://www.cnblogs.com/argenbarbie/p/3178648.html
Copyright © 2011-2022 走看看