pb_ds的优先队列实现dijkstra
用pb_ds的优先队列来做dijkstra。。据说noip能用哟。
先来篇关于仿函数的文章。
由于pb_ds支持由迭代器访问元素,并且push操作会返回一个迭代器,merge操作会更新迭代器,相当于帮你实现了根据编号找元素的功能(每个元素对应一个迭代器)。但是由于dijkstra在取出堆顶元素以后还要知道堆顶元素的编号,于是我们可以自己搞一个结构体,然后弄一个仿函数。。这样就完成了。
#include <cstdio>
#include <cstring>
#include <functional>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std; const int maxn=, maxm=5e6+, INF=; struct heap_node{
int value, id;
}; struct cmp{
bool operator() (heap_node a, heap_node b){
return a.value>b.value;
}
}; __gnu_pbds::priority_queue<heap_node, cmp> heap;
__gnu_pbds::priority_queue<heap_node, cmp>::point_iterator p[maxn]; struct Edge{
int to, next, v;
}; class Graph{
private:
int n, cntedge;
int fir[maxn];
Edge edge[maxm], link[maxn];
public:
Graph(){
n=, cntedge=;
memset(fir, , sizeof(fir));
memset(edge, , sizeof(edge));
} void addedge(int x, int y, int z){
++cntedge;
edge[cntedge].to=y;
edge[cntedge].v=z;
edge[cntedge].next=fir[x];
fir[x]=cntedge;
return;
} Edge *get_link(int x){
int nowedge, nowson, cntlink=;
for (nowedge=fir[x]; nowedge;
nowedge=edge[nowedge].next){
nowson=edge[nowedge].to;
++cntlink;
link[cntlink]=edge[nowedge];
}
link[].v=cntlink;
return link;
} }; int n, m, s;
int dis[maxn];
Graph g; int main(){
for (int i=; i<maxn; ++i)
dis[i]=INF;
scanf("%d%d%d", &n, &m, &s);
int f, gg, w;
for (int i=; i<m; ++i){
scanf("%d%d%d", &f, &gg, &w);
g.addedge(f, gg, w);
}
heap_node ht;
for (int i=; i<=n; ++i){
ht.id=i, ht.value=INF;
p[i]=heap.push(ht);
}
dis[s]=;
ht.id=s, ht.value=;
heap.modify(p[s], ht);
int now, nowson;
Edge *link;
for (int i=; i<n; ++i){
ht=heap.top();
heap.pop();
if (ht.value==INF) break;
now=ht.id;
link=g.get_link(now);
for (int i=; i<=link[].v; ++i){
nowson=link[i].to;
if (dis[now]+link[i].v<dis[nowson]){
dis[nowson]=dis[now]+link[i].v;
ht.id=nowson, ht.value=dis[nowson];
heap.modify(p[nowson], ht);
}
}
}
for (int i=; i<=n; ++i){
printf("%d ", dis[i]);
}
return ;
}
pb_ds的优先队列实现dijkstra的更多相关文章
- poj 1511 优先队列优化dijkstra *
题意:两遍最短路 链接:点我 注意结果用long long #include<cstdio> #include<iostream> #include<algorithm& ...
- POJ 1511 Invitation Cards (ZOJ 2008) 使用优先队列的dijkstra
传送门: http://poj.org/problem?id=1511 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1008 ...
- hdu 1142 用优先队列实现Dijkstra
之前很认真地看了用优先队列来实现Dijkstra这块,借鉴了小白书上的代码模板后,便拿这道题来试试水了.这道题的大意就是问你从地点1到地点2有多少条满足条件的路径(假设该路径经过 1->...- ...
- POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...
- HDU 1535 Invitation Cards(逆向思维+邻接表+优先队列的Dijkstra算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1535 Problem Description In the age of television, n ...
- Gym 101873C - Joyride - [最短路变形][优先队列优化Dijkstra]
题目链接:http://codeforces.com/gym/101873/problem/C 题意: 这是七月的又一个阳光灿烂的日子,你决定和你的小女儿一起度过快乐的一天.因为她真的很喜欢隔壁镇上的 ...
- 隐式Dijkstra:在状态集合中用优先队列求前k小
这种技巧是挺久以前接触的了,最近又突然遇到几道新题,于是总结了一下体会. 这种算法适用的前提是,标题所述的"状态集合"大到不可枚举(否则枚举就行了qaq) ...
- dijkstra算法与优先队列
这是鄙人的第一篇技术博客,作为算法小菜鸟外加轻度写作障碍者,写技术博客也算是对自己的一种挑战和鞭策吧~ 言归正传,什么是dijkstra算法呢? -dijkstra算法是一种解决最短路径问题的简单有效 ...
- POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 16178 Accepted: 526 ...
随机推荐
- 0X7FFFFFFF,0X80000000
给int类型赋值的话,0X7FFFFFFF代表最大值,0X80000000代表最小值 INT_MAX 代表最大值, INT_MIN 代表最小值 #include<iostream> #in ...
- HDU3037Saving Beans(组合数+lucas定理)
Problem Description Although winter is far away, squirrels have to work day and night to save beans. ...
- bzoj 2631: tree link-cut-tree
题目: Description 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u ...
- w3c上的SQL 教程---基本语法 语句学习
SQL 教程路径:http://www.w3school.com.cn/sql/index.asp
- css--offsetParent
offsetParent属性返回一个对象的引用,这个对象是距离调用offsetParent的元素最近的(在包含层次中最靠近的),并且是已进行过CSS定位的容器元素. 如果这个容器元素未进行CSS定位, ...
- hdu 1724 Ellipse —— 自适应辛普森积分
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1724 函数都给出来了,可以用辛普森积分: 一开始 eps = 1e-8 TLE了,答案只要三位小数,那么 ...
- bzoj 3994 约数个数和 —— 反演+数论分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3994 推导过程和这里一样:https://www.cnblogs.com/MashiroSk ...
- JavaScript-Tool:pluload
ylbtech-JavaScript-Tool:pluload Plupload是用于处理文件上传的JavaScript API,支持多文件选择.文件类型过滤.请求分块.客户端图像缩放等功能,使用不同 ...
- oracle--分页过程demo1
oracle分页过程demo1: --ROWNUM用法 select o.*,rownum rn from (select * from emp) o where rownum<=10; sel ...
- linux普通用户home目录锁定