1033. To Fill or Not to Fill (25)

1033. To Fill or Not to Fill (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
ZHANG, Guochuan

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.

Input Specification:

Each input file contains one test case. 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.

Output Specification:

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.

Sample Input 1:

 
 

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

Sample Output 1:

 
 

749.17

Sample Input 2:

 
 

50 1300 12 2 7.10 0 7.00 600

Sample Output 2:

 
 

The maximum travel distance = 1200.00


最开始题意的理解有问题,认为每次加油都要加满,用DFS遍历每种可能的加油方案,每个站点加油或者不加油,当前油可达到理解为存在边。但因为每次加油的量可以不加满,所以最后答案错误。
看了网上的文章后,才正确理解了题意,了解到这道题是所谓的贪心算法。但需要注意的是,增长的时候需要谨慎,在可达范围内,找下一个比当前点更优的点(注意是下一个更优的,不是可达范围内最优的,如果直接跳到最优点会错误),如果没找到证明当前点局部最优,所以加满,可达范围内没有点则说明不可达break。自己写的程序第二个测试点过不了,不知道是在卡什么。
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#define inf 1000000
using namespace std;
struct STATION
{
	double price;
	int dis;
	bool operator<(const STATION& s)const
	{
		return dis < s.dis;
	}
};




int main()
{
	
	int  N;
	double cap, distan, speed;
	cin >> cap >> distan >> speed >> N;
	vector<STATION>highway(N+1);
	int i, j;
	for (i = 0; i < N; i++)
		cin >> highway[i].price >> highway[i].dis;
	highway[N].dis = distan;
	highway[N].price = 0;
	sort(highway.begin(), highway.end());
	float nowcost=0,nowtank=0;
	float maxdis = 0,nowdis=0,minpri=inf,able=0;
	int next,last=0;
	i = 0;
	if (highway[0].dis != 0)printf("The maximum travel distance =0.00");
	while (i != N)
	{
		nowtank -= (highway[i].dis - highway[last].dis) / speed;
		able =highway[i].dis+ nowtank * speed;
		minpri = inf; next = -1;
		for (j = i + 1; j <= N && highway[j].dis < able; j++)
		{
			if (highway[j].price < minpri)
			{
				minpri = highway[j].price;
				next = j;
			}
		}
		if (minpri < highway[i].price);
		else
		{
			able = highway[i].dis + cap * speed;
			if (able < highway[i+1].dis)
			{
				maxdis = able;
				break;
			}
			next = -1;
			for (j = i + 1; j <= N && highway[j].dis < able; j++)
			{
				if (highway[j].price < highway[i].price)
				{
					next = j;
					break;
				}
			}
			
		    if (next!=-1)
			{
				nowcost += ((highway[next].dis - highway[i].dis) / speed - nowtank)*highway[i].price;
				nowtank = (highway[next].dis - highway[i].dis) / speed;
			}
			else
			{
				nowcost += (cap - nowtank)*highway[i].price;
				nowtank = cap;
				next = i + 1;//或许应该跳到次优点;
			}
		}
		//
		
		last = i;
		i = next;

	}
	if (maxdis != 0)printf("The maximum travel distance = %.2f", maxdis);
	else printf("%.2f", nowcost);
	
	return 0;
}

再贴一个能过的。
#include <stdio.h>  
#include <algorithm>  
using namespace std;  
  
struct Station  
{  
       double p;  
       double d;  
       bool operator <(const Station &a) const  
       {  
            return d<a.d;  
       }  
}buf[501];  
  
struct Tank  
{  
      double left;  
      double cost;   
      double d;  
}tank;  
  
  
int main()  
{  
    double Cmax,D,Davg;  
    int n;  
    while (scanf("%lf %lf %lf %d", &Cmax, &D, &Davg, &n)!=EOF)  
    {  
          for (int i=0;i<n;++i)  
          {  
              scanf("%lf %lf", &buf[i].p, &buf[i].d);  
          }  
          buf[n].d=D;  
          buf[n].p=0;  
          double farest=Cmax*Davg;  
          sort(buf, buf+n);  
          int i=0;  
          tank.left=0;  
          tank.cost=0;  
          tank.d=0;  
          if (buf[0].d!=0)  
              printf("The maximum travel distance = 0.00\n");  
          else  
          {  
                
              while (i<n)  
              {  
                  double lowest=buf[i].p;  
                  int index=i;  
                  for (int j=i+1;j<=n&&buf[j].d-buf[i].d<=tank.left*Davg;++j)  //找出油耗完前比当前站便宜的最低价站  
                      if (buf[j].p<lowest)  
                      {  
                         index=j;  
                         lowest=buf[j].p;  
                      }  
                  if (index!=i)                                               //对应上述状态1,直接开过去加油  
                  {  
                     tank.left-=(buf[index].d-buf[i].d)/Davg;  
                     i=index;  
                     tank.d=buf[i].d;               
                  }  
                  else                                                       //否则,找出满油箱可到达范围内最近的比当前站低价站  
                  {  
                      for (int j=i+1;j<=n&&buf[j].d<=buf[i].d+farest;++j)  
                      {  
                          if (buf[j].p<lowest)  
                          {  
                             index=j;  
                             break;                       
                          }      
                      }  
                      if (index!=i)                                         //对应上述状态2,加上刚好够的油开过去把油耗尽  
                      {  
                         tank.cost+=((buf[index].d-buf[i].d)/Davg-tank.left)*buf[i].p;  
                         tank.left=0;  
                         i=index;  
                         tank.d=buf[i].d;  
                      }  
                      else                                                 //否则,把油加满,然后找到满油箱可到达范围内次低价站  
                      {  
                          tank.cost+=(Cmax-tank.left)*buf[i].p;  
                          tank.left=Cmax;  
                          double low=1000000;  
                          for (int j=i+1;j<=n&&buf[j].d<=buf[i].d+farest;++j)  
                              if (buf[j].p<low)  
                              {  
                                 index=j;  
                                 low=buf[j].p;                  
                              }  
                          if (index!=i)                                    //对应上述状态3,开到次低价站  
                          {  
                            tank.left-=(buf[index].d-buf[i].d)/Davg;  
                            i=index;  
                            tank.d=buf[i].d;  
                          }  
                          else break;                                      //对应上述状态4,没有下一站  
                      }  
                  }  
              }  
              if (i==n) printf("%.2lf\n", tank.cost);  
              else     printf("The maximum travel distance = %.2lf\n", tank.d+tank.left*Davg);  
          }  
       
    }  
    return 0;  
}  

https://blog.csdn.net/u013457107/article/details/18365097

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值