用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. L100

    The world’s lightest wireless flying machine lifts off1Circult: cutting the circuitry from copper fo ...

  2. torch7 安装中Missing dependencies for nn:moses >= 1错误解决办法

    Torch7.0安装步骤(默认安装路径是在home下): git clone https://github.com/torch/distro.git ~/torch --recursive cd ~/ ...

  3. C语言小程序(八)、统计字母个数

    这么简单的程序本不应贴在这里,但每写一篇博客,积分涨10分,距离摆脱千里之外的排名又进一步,相当于刷榜了,哈哈! #include <stdio.h> #include <strin ...

  4. Gym - 101196:F Removal Game(区间DP)

    题意:一个环状数组,给定可以删去一个数,代价的相邻两个数的gcd,求最小代价. 思路:区间DP即可,dp[i][j]表示[i,j]区间只剩下i和j时的最小代价,那么dp[i][j]=min  dp[i ...

  5. [BZOJ2962][清华集训]序列操作

    bzoj luogu 题意 有一个长度为\(n\) 的序列,有三个操作: \(I \ \ a\ b\ c\ :\)表示将\([a,b]\)这一段区间的元素集体增加\(c\): \(R \ \ a\ b ...

  6. 洛谷【P1303】A*B Problem

    题目传送门:https://www.luogu.org/problemnew/show/P1303 高精度乘法板子题,灵性地回忆一下小学时期列竖式的草稿纸即可. 时间复杂度:\(O(len^2)\) ...

  7. 1. Two Sum[LeetCode 简单 by 大志]

    1. 二数之和 题目 English Given an array of integers, return indices of the two numbers such that they add ...

  8. JVM体系结构之六:堆Heap之1

    一.简介 对于大多数应用来说,Java 堆(Java Heap)是Java 虚拟机所管理的内存中最大的一块.Java 堆是被所有线程共享的一块内存区域,在虚拟机启动时创建.此内存区域的唯一目的就是存放 ...

  9. 阻止文件不被上传到iCloud

    转自:http://blog.csdn.net/a921800467b/article/details/38386787 http://www.cocoachina.com/bbs/read.php? ...

  10. stm32之UCOS-III

    一.UCOS-III 学习UCOS-III,一般会学习以下内容: 任务创建.删除.挂起.恢复等: 临界区:独占CPU,尽量少用,否则会降低效率: 时间管理:时钟节拍(基于硬件定时器).软件定时器: 互 ...