用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. Delphi中TList类应用

    在DELPHI中指针最常见的就是和类TLIST结合起来使用.下面是一个很简单的例子,希望对这个例子的分析能让大家对使用TLIST类有一个简单的认识. 代码的功能是使用指针和Tlist来生成一个牌串,并 ...

  2. css 一些技巧

    记录一下,某些实用的css: 1.以下代码将会为普通屏幕使用 image.png,为高分屏使用 image_2x.png,如果更高的分辨率则使用 image_print.png. div { back ...

  3. alt+shift+j 自动添加类的文档注释 Myeclipse

    alt+shift+j  自动添加类的文档注释 Myeclipse ctrl+shift+y 将选中的内容大写换成小写 +x是转换成大写

  4. JSTL前台报错

    报错信息: jsp页面报错 Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core&qu ...

  5. freeMarker(三)——模板开发指南之数值、类型

    学习笔记,选自freeMarker中文文档,译自 Email: ddekany at users.sourceforge.net 模板开发指南——数值.类型 1.基本内容 1.1 什么是数值? 正如你 ...

  6. ArrayList,Vector, LinkedList的存储性能和特性

    ArrayList和Vector都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内存操作,所以索引数据快而插入 ...

  7. Project Server调用PSI关闭任务以进行更新锁定任务

    /// <summary> /// 锁定和解锁项目任务 /// </summary> /// <param name="projectuid"> ...

  8. 问题:C# List;结果:C#中数组、ArrayList和List三者的区别

    C#中数组.ArrayList和List三者的区别 分类: [C#那些事] 2013-03-11 00:03 36533人阅读 评论(23) 收藏 举报 目录(?)[+] 在C#中数组,ArrayLi ...

  9. c语言基础 c和指针

    句子 c规定数组名代表数组首元素的地址 如果&a 则代表整个数组 没有内存哪来的指针 数据类型的本质:固定大小内存的别名 变量的本质:(一段连续)内存空间的别名,内存空间的标号 指针是一种数据 ...

  10. Java 数据类型间的相互转化

    Java中常见基本数据类型包括(String除外[引用]) Date(int year,int month,int day,int hour,int minute,int sec); String 格 ...