【优先队列】hdu 1434 幸福列车

用优先队列模拟:

 1 #include <iostream>
 2 #include <string>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <queue>
 6 const int MAXN = 10000+10;
 7 using namespace std;
 8
 9 typedef struct{
10     string name;
11     int rp;
12 }node;
13
14 bool operator <( node x, node y ){
15     if(x.rp>y.rp) return 1;
16     else if(x.rp==y.rp && x.name<y.name) return 1;
17     return 0;
18 }
19
20 priority_queue <node> q[MAXN];
21
22 int n,m;
23 node anode;
24 int main()
25 {
26     while(scanf("%d %d",&n,&m)!=EOF){
27         int t;
28         for(int i=1;i<=n;i++){
29             while(!q[i].empty()) q[i].pop();
30             scanf("%d",&t);
31             while(t--){
32                 cin>>anode.name>>anode.rp;
33                 q[i].push(anode);
34             }
35         }
36
37         string cmd;
38         int a,b,c;
39         for(int i=1;i<=m;i++){
40             cin>>cmd;
41             if(cmd=="GETON"){
42                 cin>>a>>anode.name>>anode.rp;
43                 q[a].push(anode);
44             }
45             else if(cmd=="JOIN")            {
46                 scanf("%d %d",&a,&b);
47                 while(!q[b].empty())                {
48                     q[a].push(q[b].top());
49                     q[b].pop();
50                 }
51             }
52             else            {
53                 scanf("%d",&a);
54                 cout<<q[a].top().name<<endl;
55                 q[a].pop();
56             }
57         }
58     }
59
60
61
62     return 0;
63 }
时间: 2024-11-01 11:35:00

【优先队列】hdu 1434 幸福列车的相关文章

HDU 1434 幸福列车

优先队列的应用 #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; #define maxn 10010 struct Node { string name; int rp; friend bool operator <(Node a,Node b) { if(a.rp != b.rp) return a.rp > b

hdu杭电1434 幸福列车【优先队列】

Problem Description 一批幸福的列车即将从杭州驶向幸福的终点站--温州,身为总列车长的linle有一些奇怪的癖好. 他会记录下全部乘客的名字(name)和他们的人品值(RP),根据这些将他们排序,并不时地从某辆列车里踢出人品最不好(RP值最低)的一个人,当两个人人品一样不好时,他就会踢出名字难听的人(linle认为按字典顺序,排在越在后面的人名字越难听). 当然出于列车行驶需要,他还会不时的发布一些命令,比如让某个乘客上车,合并某两辆列车等. linle的上一任秘书***因为不

幸福列车(优先队列模拟)

幸福列车 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Submission(s): 2169    Accepted Submission(s): 672 Problem Description 一批幸福的列车即将从杭州驶向幸福的终点站——温州,身为总列车长的linle有一些奇怪的癖好.他会记录下全部乘客的名字(name)和他们的人品值(RP),根据这些将

priority_queue 优先队列 hdu裸题。

优先队列:可以当做一个堆用,对进入此结构的类型,可以找队首的元素. 开库:#include<queue> 定义: priority_queue<type> Q; type:int类型  priority<int> Q; 默认Q.top为队列中最大的: 当我们要在队列中放入其他类型时,我们要用struct 在其中定义 operator < ; 结构如下: 定义 <int> Q.top() 为最小: struct Point { int i; bool o

hdu 2112 dijkstra+优先队列

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15727    Accepted Submission(s): 3693 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HD

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

HDU - 3790 最短路径问题(Dijkstra+优先队列优化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790 题意:中文题(边被赋予两种属性,一种是路径,一种是花费),然后略.(逃...... 今天看了卿学姐的视频,初尝SPFA和Dijkstra. 一个是用队列优化,一个是用优先队列优化.这道题目用这两种方法都可以. dijkstra算法思想(贪心):从距离起点最近的点开始,从这个点遍历一遍它周围的点,进行松弛操作,直到最终点. 整个的算法思想就是贪心,每次都给它形成最短路. 这道题目值得注意的是预处

hdu 1242 Rescue(bfs+优先队列)

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approach Angel. We assume

HDU 3152 Obstacle Course(BFS+优先队列 重载)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3152 Problem Description You are working on the team assisting with programming for the Mars rover. To conserve energy, the rover needs to find optimal paths across the rugged terrain to get from its sta