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

      

  • 相关阅读:
    【梦断代码】与我们队的相似之处
    梦断代码 之 你失败过吗
    梦断代码 之 程序人生
    C#中父类转换为子类
    C#中Dictionary泛型集合7种常见的用法
    Linux 常见命令 目录处理指令
    使用XSLT+XML生成网页
    我心目中的Asp.net核心对象
    配色速成
    VS.NET中JavaScript隐藏特性
  • 原文地址:https://www.cnblogs.com/argenbarbie/p/3178648.html
Copyright © 2011-2022 走看看