• 九度 题目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;
    }
    

      

  • 相关阅读:
    2021年通达信指标公式大全,值得收藏!
    网络兼职?威客?为什么我会觉得网络兼职,威客会是人生中应该具备的一种能力!
    SeMusic 音乐网站源代码,PHP音乐系统,人人都是站长人人都可副业创业!
    JavaScript 查看图片,带缩放放大效果
    JS (javascript) 计算循环当前时间,javascript 时间钟表
    关键词被冷藏?关键词没排名?任务网站长们该何去何从?
    关键词任务网被K,对于任务网该何去何从?我认为任务网存活只有一条出路!
    C3属性的轮播图(持续更新)
    自己写的文字轮播(简陋版)
    带锁的3D切割轮播图
  • 原文地址:https://www.cnblogs.com/argenbarbie/p/3178648.html
Copyright © 2020-2023  润新知