结构体优先队列的用法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了结构体优先队列的用法相关的知识,希望对你有一定的参考价值。

今天做一个微软的校招笔试题 Registration Day ,用优先队列模拟操作的。粘贴来别人的代码,谨记 pq 的用法。另外 memset 包含在 string.h 里。

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <limits.h>
 4 #include <queue>
 5 #include <vector>
 6 #include <set>
 7 #include <map>
 8 #include <algorithm>
 9 
10 using namespace std;
11 
12 int N, M, K;
13 struct Student
14 {
15     int id;
16     int arrive_time;
17     int office_num;
18     int finish_time;
19     std::vector<pair<int, int> > register_offices;
20 };
21 Student students[10005];
22 struct Event
23 {
24     int student_idx;
25     int office;
26     int begin;
27     int duration;
28     Event(int s, int o, int b, int d): student_idx(s), office(o), begin(b), duration(d){};
29 };
30 struct Cmp
31 {
32     bool operator()(const Event &e1, const Event &e2){
33         if (e1.begin == e2.begin)
34             return students[e1.student_idx].id > students[e2.student_idx].id;
35         return e1.begin > e2.begin;
36     }
37 };
38 priority_queue<Event, vector<Event>, Cmp> pq;
39 int pre[105];
40 int pos[10005];
41 void Solve() {
42     for (int i=0;i<N;++i) {
43         pq.push(Event(i, students[i].register_offices[0].first, students[i].arrive_time+K, students[i].register_offices[0].second));
44         pos[i] = 1;
45     }
46     memset(pre, -1, sizeof(pre));
47     while (!pq.empty()) {
48         Event e = pq.top();
49         pq.pop();
50         int st = e.student_idx;
51         if (pre[e.office] > e.begin) {
52             e.begin = pre[e.office];
53         }
54         int finish_time = e.begin + e.duration;
55         if (pos[st] == students[st].office_num) {
56             students[st].finish_time = finish_time;
57         } else {
58             int office = students[st].register_offices[pos[st]].first;
59             int duration = students[st].register_offices[pos[st]].second;
60             pq.push(Event(st, office, finish_time+K, duration));
61             pos[st]++;
62         }
63         pre[e.office] = finish_time;
64     }
65 }
66 int main()
67 {
68     scanf("%d%d%d", &N, &M, &K);
69     int p, o, w;
70     for (int i=0;i<N;++i) {
71         scanf("%d%d%d", &students[i].id, &students[i].arrive_time, &p);
72         students[i].office_num = p;
73         for (int j=0;j<p;++j) {
74             scanf("%d%d", &o, &w);
75             students[i].register_offices.push_back(make_pair(o, w));
76         }
77     }
78     Solve();
79     for (int i=0;i<N;++i) {
80         printf("%d\n", students[i].finish_time);
81     }
82     return 0;
83 }

以下是转载的 functional 模板的优先队列用法,以供方便做题查找

 1 #include<iostream>  
 2 #include<functional>  
 3 #include<queue>  
 4 using namespace std;  
 5 struct node  
 6 {  
 7     friend bool operator< (node n1, node n2)  
 8     {  
 9         return n1.priority < n2.priority;  //"<"为从大到小排列,">"为从小到大排列  
10     }  
11     int priority;  
12     int value;  
13 };  
14 int main()  
15 {  
16     const int len = 5;  //也可以写在函数内  
17     int i;  
18     int a[len] = {3,5,9,6,2};  
19     //示例1  
20     priority_queue<int> qi;  //普通的优先级队列,按从大到小排序  
21     for(i = 0; i < len; i++)  
22         qi.push(a[i]);  
23     for(i = 0; i < len; i++)  
24     {  
25         cout<<qi.top()<<" ";  
26         qi.pop();  
27     }  
28     cout<<endl;  
29     //示例2  
30     priority_queue<int, vector<int>, greater<int> > qi2;  //从小到大的优先级队列,可将greater改为less,即为从大到小  
31     for(i = 0; i < len; i++)  
32         qi2.push(a[i]);  
33     for(i = 0; i < len; i++)  
34     {  
35         cout<<qi2.top()<<" ";  
36         qi2.pop();  
37     }  
38     cout<<endl;  
39     //示例3  
40     priority_queue<node> qn;  //必须要重载运算符  
41     node b[len];  
42     b[0].priority = 6; b[0].value = 1;  
43     b[1].priority = 9; b[1].value = 5;  
44     b[2].priority = 2; b[2].value = 3;  
45     b[3].priority = 8; b[3].value = 2;  
46     b[4].priority = 1; b[4].value = 4;  
47    
48     for(i = 0; i < len; i++)  
49         qn.push(b[i]);  
50     cout<<"优先级"<<\t<<""<<endl;  
51     for(i = 0; i < len; i++)  
52     {  
53         cout<<qn.top().priority<<\t<<qn.top().value<<endl;  
54         qn.pop();  
55     }  
56     return 0;  
57 } 

 

以上是关于结构体优先队列的用法的主要内容,如果未能解决你的问题,请参考以下文章

优先队列下的迪杰斯特拉算法(结构体与队列的冲突)

带结构体的优先队列

hdu1873 看病要排队(结构体优先队列)

priority_queue 结构体的优先级设置

hdu1873-看病要排队-(结构体优先队列)

优先队列问题 get it !!