POJ: 2413 ——优先队列——结构体cmp函数一级排序

我要翻译题目!!!

/*

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 fuel tank. The truck now leaks one unit of fuel every unit of distance it travels.

有一群奶牛拖着一辆货车,准备进行一个进入丛林深处进行考察的探险。因为那个该死的司机,现在奶牛们被要求去拉一块大石头,并且他们将货车的油箱捅破了。现在油箱每走一个旅途中的单位就会泄漏一单位的燃料。

To repair the truck, the cows need to drive to the nearest town (no more than 1,000,000 units distant) down a long, winding road. On this road, between the town and the current location of the truck, there are N (1 <= N <= 10,000) fuel stops where the cows can stop to acquire additional fuel (1..100 units at each stop).

为了去修补油箱,奶牛们需要尽快将货车拉到最近的城镇(不超过1000000个距离单位),并且经过一条又长又曲折的道路。在这条路上,奶牛们所处的具体位置和城镇之间隔着N个加油站。他们可以在那里得到任意数量的燃料(数据会在1-100之间)

The jungle is a dangerous place for humans and is especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the way to the town. Fortunately, the capacity of the fuel tank on their truck is so large that there is effectively no limit to the amount of fuel it can hold. The truck is currently L units away from the town and has P units of fuel (1 <= P <= 1,000,000).

丛林对于人类来说是一个危险的地方,对于奶牛们来说更是如此。因此,奶牛们想要经过最少的加油站(在加油站加油也是需要时间的对吧?)。幸运的是,货车油箱是容量很大。现在货车处于具体城镇L单位的距离上,并且此时车上有P单位的燃料。

Determine the minimum number of stops needed to reach the town, or if the cows cannot reach the town at all.

算出他们需要经过的最少加油站,如果不能到达城镇,就输出-1.

*/

有点机器翻译的感觉,不过真的是我翻译的 T ,T

代码已经在POJ上AC了,思路我参考了资料,但是每一行代码都是我经过思考后写下来的。我会在那些要注意的地方写下我的注释。而且代码有两个版本,一个是使用pair<>来对结构体进行排序,一个是用cmp函数来进行排序。我只贴上cmp的那份。

 /*  思路:在路程不变的情况下,要让加油的次数最少,就必须每次尽量多得加油。在计划加油站点的时候,如果发现现在的油量无法使他们到达下一站时,就在前面出现过的可加油量最多的加油站进行加油。这样就不会出现在下一站无法到达的情况。于是可以使用优先队列进行运算。*/ 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <queue>
 4 #define max_n 10000+5
 5 using namespace std;
 6
 7 struct dis_fuel{
 8     int dis;
 9     int fuel;
10 };  /*    cmp(定义两个空指针,const void *a,const void *b)    强制转换两个空指针为dis_fuel型,并指向结构体中的结构成员dis。若a>b,则返回1,若a<b则返回-1,进行升序处理。  */
11 int cmp(const void *a, const void *b){
12     return *(dis_fuel *a).dis > *(dis_fuel *b) ? 1 : -1;15 }
16 int main()
17 {
18     //建立优先队列
19     priority_queue <int> que;
20
21     struct dis_fuel AB[max_n];
22
23     int num,A[max_n],B[max_n],L,P;
24     scanf("%d",&num);
25
26     for(int i = 0;i < num;i++){
27         scanf("%d %d",&A[i],&B[i]);
28     }
29     scanf("%d %d",&L,&P);
30
31     for(int i = 0;i < num;i++){
32         AB[i].dis = L - A[i];
33         AB[i].fuel = B[i];
34     }    /*      用qsort进行对结构体进行排序。格式为 qsort(结构数组名,数组的个数,数组元素的内存所占空间,函数名);    */
35     qsort(AB,num,sizeof(AB[0]),cmp);
36
37     AB[num].dis = L;
38     AB[num].fuel = 0;
39     num++;
40
41     int tank = P,pos = 0,ans = 0,dis_;
42    /*     在for循环里面对每一个每个currently ponit 进行遍历。如果当前的油量不能保持他走到下一站,则停下来进行加油。此时的油量是前面出现加油站提供的油量的最大     值。如果能,则继续走,并把每个加油站能提供的油量放进优先队列里面。   */
43     for(int i = 0;i < num;i++){
44         dis_ = AB[i].dis - pos;
45
46         while(dis_ > tank){
47             if(que.empty()){
48                 printf("-1\n");
49                 return 0;
50             }
51             tank += que.top();
52             que.pop();
53             ans++;
54         }
55
56         tank -= dis_;
57         pos = AB[i].dis;
58         que.push(AB[i].fuel);
59     }
60
61     printf("%d\n",ans);
62
63     return 0;
64
65 }
时间: 2024-08-13 17:06:40

POJ: 2413 ——优先队列——结构体cmp函数一级排序的相关文章

C语言----结构体---结构体与函数

结构作为参数的函数 整个结构可以作为参数传入函数 这时是在函数中新建了一个结构变量,并复制调用这个结构的值(重点,只是把值传入函数,而函数外面真正的变量并没有改变,与数组不同) 函数也可以返回一个结构 直接来个简单的例子吧: 问题:用户输入今天的日期,输出明天的日期. 提示:闰年,每个月最后一天, 代码: #include <stdio.h>#include <stdbool.h>/* 根据今天的日期算出明天的日期.*/ //结构体放在函数外侧,相当于一个全局变量,所有的函数都能使

【学习笔记】【C语言】结构体和函数

1 #include <stdio.h> 2 struct Student 3 { 4 int age; 5 int no; 6 }; 7 8 // 如果结构体作为函数参数,只是将实参结构体所有成员的值对应地赋值给了形参结构体的所有成员 9 // 修改函数内部结构体的成员不会影响外面的实参结构体 10 void test(struct Student s) 11 { 12 s.age = 30; 13 s.no = 2; 14 } 15 16 // 会影响外面的实参结构体 17 void te

C语言中结构体在函数中的应用

一.结构体与函数参数结构体作函数参数可分为传值与传指针.1.传值时结构体参数会被拷贝一份,在函数体内修改结构体参数成员的值实际上是修改调用参数的一个临时拷贝的成员的值,这不会影响到调用参数.在这种情况下,由于涉及到结构体参数的拷贝,程序空间及时间效率都会受到影响,所以这种方法基本不用.例如:typedef struct tagSTUDENT{char name[20];int age;}STUDENT; void fun(STUDENT stu){printf("stu.name=%s,stu.

ffmpeg中对AVInputFormat结构体中函数的调用

http://blog.csdn.net/junllee/article/details/7722605 opt_input_file()->      avformat_open_input()->             init_input()->                   av_probe_input_format()->                          av_probe_input_format2()->                 

返回结构体的函数

代码如下: 1 //返回结构体的函数 2 #include<stdio.h> 3 float a[10]; 4 struct b 5 { 6 float sum; 7 float max; 8 float min; 9 }; 10 b B; 11 b Select(float a[],int); 12 int main() 13 { 14 for(int i=0;i<10;i++) scanf("%f",&a[i]); 15 B=Select(a,10); 1

C语言结构体及函数传递数组参数示例

注:makeSphere()函数返回Sphere结构体,main函数中,调用makeSphere()函数,传递的第一个参数为数组,传递的数组作为指针. 版权声明:本文为博主原创文章,未经博主允许不得转载.

结构体中函数指针与typedef关键用途(函数指针)

// 结构体函数指针.  #include<stdio.h> //为了代码的移植考虑,一般使用typedef定义函数指针类 ,另一个好处是,减少代码的书写量.  typedef void (*shout)(char *name,int age); typedef struct {  //用指针来存储字符串     char *name;    int age ;    shout personinfo; }person; //类似于c++中类方法的实现,在这里,是为结构体中指针函数提供实现.在

C语言结构体及函数传递数组參数演示样例

注:makeSphere()函数返回Sphere结构体,main函数中.调用makeSphere()函数,传递的第一个參数为数组,传递的数组作为指针.

C语言 结构体作为函数的参数

1)使用结构体变量作为函数的参数 使用结构体变量作为函数的实参时,采用的是值传递,会将结构体变量所占内存单元的内容全部顺序传递给形参,形参必须是同类型的结构体变量 demo: 1 # include <stdio.h> 2 # include <stdlib.h> 3 4 //创建一个Student结构 5 struct Student 6 { 7 char name[30]; 8 float fScore[3]; 9 }student={"dire",98.5