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.

  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 ****************************************************************/

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

原题是POJ 2431

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

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

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

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

实际上是一个贪心问题,

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

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

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

时间: 2024-12-28 21:09:54

To Fill or Not to Fill(贪心模拟)的相关文章

PAT 1033. To Fill or Not to Fill (25)(贪心)

解题思路: 其实题目本身不难,难就难在对贪心策略的选取 在每一个加油点应该做这样的判断: 1.首先计算从本加油点做多可能达到的加油点 2.如果有可达加油点的话: 2.1 寻找有无比当前加油点油价便宜的加油点,如果有的话就跑到该便宜的加油点,油量加到支持到该加油点即可 2.2 如果没有比当前加油点更便宜的加油点的话,又分两种情况: 2.2.1.如果本次加油可能直接到终点,就加可以到达终点的油量 2.2.2.否则的话,加满 3.如果没有可达加油点的话: 3.1 看是否可以直接达到终点 3.2 不能达

【贪心】PAT 1033. To Fill or Not to Fill (25)

1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 32000 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 fin

九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题

题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: 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 fro

1033 To Fill or Not to Fill (25 分)

1033 To Fill or Not to Fill (25 分) 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 g

[贪心+模拟] zoj 3829 Known Notation

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5383 Known Notation Time Limit: 2 Seconds      Memory Limit: 65536 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science.

POJ 1979 POJ 3009 AOJ 0033 AOJ 0118 [搜索类题目][0033贪心模拟]

/** POJ 1979 BFS */ #include <stdio.h> #include <string.h> #include <iostream> #include <queue> using namespace std; const int N = 20 + 5; int mp[N][N]; int sx,sy; int n, m; int vis[3000]; int dirx[] = {0, 1, 0, -1}; int diry[] = {

pat1033. To Fill or Not to Fill (25)

1033. To Fill or Not to Fill (25) 时间限制 10 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 fin

hdu 4915 Parenthese sequence (贪心+模拟)

题目大意: 一个序列中有左括号和右括号,还有问号,问号可以任意转换成左右括号. 问这个序列有多少种情况的转变使得这个序列变成合法的括号匹配序列. 思路分析: 首先我们分析一下,如何使得一个序列是合法的括号匹配序列. 我们很容易想到的是用栈模拟匹配过程. 当遇到左括号就进栈,当遇到右括号就让栈顶的左括号出栈. 那么在模拟的过程中,造成这个序列的不合法的原因只有当右括号来的时候,此时的栈已经为空. 这里补充一句,一旦一个序列给定,那么这里面的问号有多少变成左括号,多少变成右括号,是一定的. 看完以上

PAT 1033 To Fill or Not to Fill[dp]

1033 To Fill or Not to Fill(25 分) 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 gi

1002 Game (贪心模拟)

1002 Game (贪心模拟)Accepts: 572 Submissions: 6218 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description 度度熊在玩一个好玩的游戏. 游戏的主人公站在一根数轴上,他可以在数轴上任意移动,对于每次移动,他可以选择往左或往右走一格或两格. 现在他要依次完成 n 个任务,对于任务 i,只要他处于区间 [ai,bi]