用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的更多相关文章

  1. poj 1511 优先队列优化dijkstra *

    题意:两遍最短路 链接:点我 注意结果用long long #include<cstdio> #include<iostream> #include<algorithm& ...

  2. POJ 1511 Invitation Cards (ZOJ 2008) 使用优先队列的dijkstra

    传送门: http://poj.org/problem?id=1511 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1008 ...

  3. hdu 1142 用优先队列实现Dijkstra

    之前很认真地看了用优先队列来实现Dijkstra这块,借鉴了小白书上的代码模板后,便拿这道题来试试水了.这道题的大意就是问你从地点1到地点2有多少条满足条件的路径(假设该路径经过 1->...- ...

  4. POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]

    题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...

  5. HDU 1535 Invitation Cards(逆向思维+邻接表+优先队列的Dijkstra算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1535 Problem Description In the age of television, n ...

  6. Gym 101873C - Joyride - [最短路变形][优先队列优化Dijkstra]

    题目链接:http://codeforces.com/gym/101873/problem/C 题意: 这是七月的又一个阳光灿烂的日子,你决定和你的小女儿一起度过快乐的一天.因为她真的很喜欢隔壁镇上的 ...

  7. 隐式Dijkstra:在状态集合中用优先队列求前k小

    这种技巧是挺久以前接触的了,最近又突然遇到几道新题,于是总结了一下体会. 这种算法适用的前提是,标题所述的"状态集合"大到不可枚举(否则枚举就行了qaq) ...

  8. dijkstra算法与优先队列

    这是鄙人的第一篇技术博客,作为算法小菜鸟外加轻度写作障碍者,写技术博客也算是对自己的一种挑战和鞭策吧~ 言归正传,什么是dijkstra算法呢? -dijkstra算法是一种解决最短路径问题的简单有效 ...

  9. POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 16178   Accepted: 526 ...

随机推荐

  1. 机器学习(二十四)— 偏差Bias 与方差Variance

    1.首先 Error = Bias + Variance  Error反映的是整个模型的准确度, Bias反映的是模型在样本上的输出与真实值之间的误差,即模型本身的精准度, Variance反映的是模 ...

  2. hadoop_学习_01_入门准备

    一.入门准备 1.零基础学习Hadoop 2.大数据初学者应该知道的知识

  3. 重拾安卓_01_安卓开发环境搭建(eclipse)

    一.下载安装Android SDK 1.下载地址 (1)官网(可FQ选择):http://developer.android.com/sdk/index.html (2)不可FQ选择:http://w ...

  4. leetcode 3 Longest Substring Without Repeating Characters(滑动窗口)

    用滑动窗口的思想来做.用一个unordered_map来查询之前的char有没有在现在的窗口中. class Solution { public: int lengthOfLongestSubstri ...

  5. stl_multimap.h

    stl_multimap.h // Filename: stl_multimap.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: ...

  6. FEC之我见三

    继续上文讲解: 3) 标准的RTP头结构如下所示: 其中第一个字节中的x标志位是否扩展了RTP头,RTP协议允许用户自定义的扩展,扩展的字段紧挨上述RTP固定头.RTP扩展投中承载如下信息: 1).当 ...

  7. 【C&C++】查看代码运行时间

    查看代码运行时间有助于更好地优化项目代码 1. Windows平台 windows平台下有两种方式,精度有所不同,都需要包含<windows.h>头文件 1) DWORD GetTickC ...

  8. http之pragma

    关于Pragma:no-cache,跟Cache-Control: no-cache相同.Pragma: no-cache兼容http 1.0 ,Cache-Control: no-cache是htt ...

  9. poj 1517 u Calculate e(精度控制+水题)

    一.Description A simple mathematical formula for e is e=Σ0<=i<=n1/i! where n is allowed to go t ...

  10. Python:更改字典的key

    思路:先删除原键值对,保存值,然后以新键插入字典 格式:dict[newkey] = dict.pop(key) d = {'a':1, 'aa':11} d['b'] = d.pop('a') d[ ...