Heap:Expedition(POJ 2431)

                  

                  远征队

  题目大意:一部车要从一个地方走到另一个地方,开始的时候车的油箱有P升油,汽车每走1个距离消耗1升油,没有油汽车无法行驶,路上有加油站,可以为汽车加油,设汽车的油缸是无限大小的,问你汽车能否走到终点?如果可以,需要用到最小的加油站的数目是多少?

  这一题可以这么理解,因为我们只用最小的加油站数目,那么我们可以只用每次都加最大油量就可以了,我们可以认为汽车经过的加油站都像势能一样储存起来,随时可以加油

  那么这样过后,我们只用维护一个最大堆就可以了,也算是一个贪婪算法吧

  

 1 #include <iostream>
 2 #include <functional>
 3 #include <queue>
 4 #define MAX_N 10001
 5
 6 using namespace std;
 7
 8 typedef struct gas
 9 {
10     int gas_pos;
11     int gas_sum;
12 }GS;
13
14 GS state[MAX_N];
15 void Search(const int, const int, const int);
16 int fcmop(const void *a, const void *b)
17 {
18     return (*(GS *)b).gas_pos - (*(GS *)a).gas_pos;
19 }
20
21 int main(void)
22 {
23     int N, L, P;
24     while (~scanf("%d", &N))
25     {
26         for (int i = 0; i < N; i++)//读入加油站的油量
27             scanf("%d%d", &state[i].gas_pos, &state[i].gas_sum);
28         scanf("%d%d", &L, &P);
29         state[N].gas_pos = 0; state[N].gas_sum = 0;//把终点看成油站
30         qsort(state, N, sizeof(GS), fcmop);//记得成从远到近(距离终点)
31         Search(L, N, P);
32     }
33     return 0;
34 }
35
36 void Search(const int L, const int N, const int P)
37 {
38     priority_queue < int, vector<int>, less<int> > que;
39     int i, ans = 0, tank = P, now_state = 0 , dist;
40
41     for (i = 0; i <= N; i++)
42     {
43         dist = (L - state[i].gas_pos) - now_state;//下一个油站到现在的距离
44         while (tank - dist < 0)//如果不支持到
45         {
46             if (que.empty())//没有油站给加油了,直接不能走到终点
47             {
48                 cout << -1 << endl;
49                 return;
50             }
51             tank += que.top();
52             que.pop();
53             ans++;
54         }
55         que.push(state[i].gas_sum);//相当于是势能,储存起来
56         tank -= dist;
57         now_state = (L - state[i].gas_pos);
58     }
59     cout << ans << endl;
60 }
时间: 2024-11-06 18:15:04

Heap:Expedition(POJ 2431)的相关文章

Expedition POJ 2431

1.题目描述:点击打开链接 2解题思路:本题利用优先队列解决.本题在思维上需要稍微灵活变通一下:如果到达第i个加油站时,把它能够提供的油量预存储到队列中,等将来真正需要加油的时候,再从队列中取出来.这就等价于在到达第i个加油站时选择了加油的措施.不过本题要求加油次数最少.这时我们可以考虑使用STL中的优先队列解决.每次都选加油量最大的那个加油站来加油.这样的贪心选择可以保证最终的加油次数是最少的. 3.代码: #define _CRT_SECURE_NO_WARNINGS #include<io

poj 2431 Expedition (贪心+优先队列)

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6890   Accepted: 2065 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to

poj 2431 Expedition 贪心+最大堆

当油量不够时从走过的油站中选最大加油量的 #include<iostream> #include<queue> #include<stdlib.h> #include<algorithm> using namespace std; #define MAX_N 10005 struct node{ int dist,fuel; }t[MAX_N]; bool cmp(const node &a,const node &b) { return a

[POJ 2431]Expedition

1.题目连接:http://poj.org/problem?id=2431 2.题目大意:你需要开着一辆卡车行驶L单位距离,最开始卡车有P单位汽油,卡车每开一单位距离需要消耗1单位汽油,如果在中途卡车汽油耗尽,卡车就无法继续前行,到不了终点,在途中一共有n个加油站,告诉你每个加油站距离终点的距离和每个加油站可以加的油量,假设卡车的油箱是无穷大的,问卡车至少要加多少次油才能到达终点?卡车到不了终点输出-1 3.题目思路:使用优先队列+贪心思想完成此题,队列中维护到达目前的加油站之前没有用过的加油站

POJ 2431 Expedition (STL 优先权队列)

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8053   Accepted: 2359 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to

poj 2431 Expedition(优先队列)

题目链接 http://poj.org/problem?id=2431 Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9243   Accepted: 2700 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor driver

poj - 2431 Expedition (优先队列)

http://poj.org/problem?id=2431 你需要驾驶一辆卡车做一次长途旅行,但是卡车每走一单位就会消耗掉一单位的油,如果没有油就走不了,为了修复卡车,卡车需要被开到距离最近的城镇,在当前位置和城镇之间有n个加油站可以加油. 为了减少危险,需要最少的加油次数,卡车的油箱可以看作无限大,卡车初始距离城镇L单位,自身有P单位的油. 注意输入的距离是与城镇的距离不是与开始点的距离.转换一下就好. 思想:把经过的所有加油站都加入优先队列,当燃料不足时就取出优先队列的最大元素,用来给卡车

POJ 2431 Expedition 贪心 优先级队列

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30702   Accepted: 8457 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed t

POJ 2431 Expedition(探险)

Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck's f