九度OJ 题目1437:To Fill or Not to Fill

来源:互联网 发布:opencv python puttext 编辑:程序博客网 时间:2024/06/06 05:51

一题目描述:
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 <stdio.h>#include <stdlib.h>#define MAXN 501#define INF 999999 typedef struct Sta{    double dist;    double perprice;}Station;int cmp(const void*a,const void*b){    return (*(Station*)a).dist-(*(Station*)b).dist;}int main(){    double Cmax,D,Davg,sumprice,maxdist,curgas;    int N,i,j;    Station S[MAXN];    while(scanf("%lf%lf%lf%d",&Cmax,&D,&Davg,&N)!=EOF)    {        for(i=0;i<N;i++)            scanf("%lf%lf",&S[i].perprice,&S[i].dist);        S[N].dist=D;        S[N].perprice=INF;        qsort(S,N+1,sizeof(S[0]),cmp);        if(S[0].dist>0)        {            printf("The maximum travel distance = 0.00\n");            continue;        }        //init        sumprice=0;        maxdist=Cmax*Davg;        curgas=0;        for(i=0;i<N;i++)        {            //for station i,first comupte current gas,then compute if need add gas,update current gas            if(i!=0)                curgas -= (S[i].dist-S[i-1].dist)/Davg;            if((S[i+1].dist-S[i].dist)>maxdist)            {                printf("The maximum travel distance = %.2f\n",S[i].dist+Cmax*Davg);                break;            }            //for i ,there are three continues            //find minner price j            for(j=i+1;j<N&&S[j].perprice>=S[i].perprice;j++)                continue;            //to judje if j is arriveded            // arrived            if((S[j].dist-S[i].dist)<maxdist)            {                //if gas is not enough                if(curgas*Davg<(S[j].dist-S[i].dist))                {                    sumprice += ((S[j].dist-S[i].dist)/Davg-curgas)*S[i].perprice;                    curgas = (S[j].dist-S[i].dist)/Davg;                }                //else gas is enough,go on            }            // canot find j ,then add full gas at i            else            {                sumprice += (Cmax-curgas)*S[i].perprice;                curgas = Cmax;            }        }        if(i==N)            printf("%.2f\n",sumprice);    }    return 0;}



0 0
原创粉丝点击