浙大PAT甲级 1033

贪心题,首先先判断是否能到达目的地,有两点:

(1)如果在0的位置上没有加油站,则不能达到目的地,最大距离为0.00。

(2)如果有两个加油站的距离之差大于cmax*ave或者目的地与最远加油站的距离也大于cmax*ave,则也不能到达目的地,最大距离为前者加油站的位置+cmax*ave。

排除以上两种情况,则接下来是能够到达的。

那么该题使用贪心算法,先按照价格对加油站进行排序。为了使价格最少,尽可能地多在便宜的加油站处加满油,而到达该站时油已经耗尽。因此可以用一个标记数组mark[30005]来对路进行标记,如果行驶过则标记1,如果遇到该路的标记为1,则不进行计算。

AC代码:

#include
#include
#include
#include
#include
#include
#include
#include
#define inf 10000000
using namespace std;
int mark[30005];
struct station
{
    double cost;
    int pos;
};
station a[505];
bool cmp(station x,station y)
{
    return x.cost>c>>dis>>ave>>n;
    for(int i=0;i>a[i].cost>>a[i].pos;
    }
    sort(a,a+n,cmp1);
    if(a[0].pos!=0)
    {
        cout<<"The maximum travel distance = 0.00";
        return 0;
    }
    for(int i=0;ic*ave)
        {
            printf("The maximum travel distance = %.2lf",(double)(a[i].pos+c*ave));
            return 0;
        }
    }
    if(dis-a[n-1].pos>c*ave)
    {
         printf("The maximum travel distance = %.2lf",(double)(a[n-1].pos+c*ave));
         return 0;
    }
    sort(a,a+n,cmp);
    double ans=0;
    for(int i=0;i


你可能感兴趣的:(浙大pat)