码迷,mamicode.com
首页 > 其他好文 > 详细

To Fill or Not to Fill(贪心模拟)

时间:2016-06-27 00:04:30      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

题目描述:

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.

技术分享
  1 #include<stdio.h>
  2 #include<algorithm>
  3 #include<iostream>
  4 #include<cstring>
  5 using namespace std;
  6  
  7 typedef struct{
  8     double pri;
  9     int dis;
 10 }Station;
 11  
 12 bool cmp(const Station &A, const Station &B){
 13     if(A.dis!=B.dis)
 14         return A.dis<B.dis;
 15     else
 16         return A.pri<B.pri;
 17 }
 18 int Cmax, D, Davg, tmp1;
 19 int N, i, j, tmp_ind;
 20 double tank, now_pos, now_pri, tot_cost, comp, tmp_pos;
 21 bool flag, flag2, flag3, flag4;
 22 Station S[550];
 23 int main(){
 24     while(~scanf("%d%d%d%d",&Cmax,&D,&Davg,&N)){
 25         memset(S,0,sizeof(S));
 26         tank=0;
 27         now_pos=0;
 28         tot_cost=0;
 29         for(i=0;i<N;i++)
 30             scanf("%lf%d",&S[i].pri,&S[i].dis);
 31         sort(S,S+N,cmp);
 32         S[N].dis=D;
 33         S[N].pri=0;
 34         if(S[0].dis!=0||N==0){
 35             printf("The maximum travel distance = 0.00\n");
 36             continue;
 37         }
 38         for(i=0;i<N;i++){
 39             flag=false;
 40             flag2=false;
 41             flag3=false;
 42             flag4=false;
 43             if(i+1<N){
 44                 comp=S[i+1].pri;
 45                 tmp_pos=S[i+1].dis;
 46             }
 47             for(j=i+1;j<=N;j++){
 48                 if(S[j].dis<=now_pos+Cmax*Davg&&S[j].dis>=now_pos){
 49                     flag=true;
 50                     if(S[j].pri<S[i].pri){
 51                         if(S[j].dis<=now_pos+tank*Davg){
 52                             //价格比较低,现有的油可以开到那里
 53                             tank-=(S[j].dis-now_pos)/Davg;
 54                             now_pos=S[j].dis;
 55                         }
 56                         else{
 57                             //价格比较低,到那里再加油
 58                             tot_cost+=S[i].pri*((S[j].dis-now_pos)/Davg-tank);
 59                             tank=0;
 60                             now_pos=S[j].dis;
 61                         }
 62                         i=j-1;
 63                         flag2=true;
 64                         break;
 65                     }
 66                     else{
 67                         flag4=true;
 68                         if(S[j].pri<=comp){
 69                             comp=S[j].pri;
 70                             tmp_pos=S[j].dis;
 71                             tmp_ind=j;
 72                         }
 73                     }
 74                 }
 75             }
 76             if(flag2)
 77                 continue;
 78             if(now_pos+Cmax*Davg>=tmp_pos){
 79                 //可开到的加油站中价格相对较低,在本地加满油,开到该加油站
 80                 tot_cost+=(double)(Cmax-tank)*S[i].pri;
 81                 tank=(double)(Cmax-(double)(tmp_pos-now_pos)/Davg);
 82                 now_pos=tmp_pos;
 83             }
 84             else{
 85                 //开不到该加油站
 86                 now_pos+=Cmax*Davg;
 87                 tot_cost+=(double)(Cmax-tank)*S[i].pri;
 88                 printf("The maximum travel distance = %.2lf\n",now_pos);
 89                 flag3=true;
 90                 break;
 91             }
 92             if(flag4)
 93                 i=tmp_ind-1;
 94             if(!flag&&now_pos<(double)D){
 95                 printf("The maximum travel distance = %.2lf\n",now_pos+tank*Davg);
 96                 flag3=true;
 97                 break;
 98             }
 99         }
100         if(!flag3)
101             printf("%.2lf\n",tot_cost);
102     }
103 }
104  
105 /**************************************************************
106     Problem: 1437
107     User: blueprintf
108     Language: C++
109     Result: Accepted
110     Time:10 ms
111     Memory:1532 kb
112 ****************************************************************/
View Code

题目咋一看感觉好像是用优先队列去做,但是和那个题又有些不同,

原题是POJ 2431

题意是有N个加油站,要走L单位距离,问卡车是否能够到达终点,如果可以,最少的加油次数?

当油箱没油的时候,从大到小取出已路过的加油站中可加油量最大的加油站,利用优先队列进行

本题的题意是有N个加油站,但是油箱有上限,

问是否能到达终点,若不能到达,输出最远距离,若能到达,输出最小花费。

实际上是一个贪心问题,

如果在加满油可到达的区域内有加油站比此处的加油站便宜,加最少的油到达第一个便宜的加油站;

否则,在该地加满油,再到相对最便宜的的加油站进行下一步选择。

 

敲代码的时候出了点问题,需要判断什么时候到达不了终点。

To Fill or Not to Fill(贪心模拟)

标签:

原文地址:http://www.cnblogs.com/blueprintf/p/5618869.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!