Dijkstra的优先队列
模板
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<cstdio>
#include<map>
#include<set>
#include<string>
#include<queue>
using namespace std;
#define inf 2147483647
typedef long long ll;
const ll mod=1e9+;
const int maxn=+;
ll dis[maxn];
struct edg{
int to;
int w;
};
vector<edg > sm[maxn];
typedef pair<int ,int > P;//最短距离,标点
void dij(int v){
fill(dis,dis+maxn,-);
priority_queue<P,vector<P>,greater<P> > que;
que.push(P(,v));
P tem;
dis[v]=;
while(!que.empty()){
tem=que.top();
que.pop();
int i=tem.second;
if(tem.first>dis[i])
continue;
for(int j=;j<sm[i].size();j++){
edg e=sm[i][j];
if(dis[e.to]==-||dis[e.to]>dis[i]+e.w){
dis[e.to]=dis[i]+e.w;
que.push(P(dis[e.to],e.to));
}
}
}
}
int main(){
int n,m,s,t;
int u,v;
ll w;
ll ans=;
edg tem;
cin>>n>>m>>s>>t;
for(int i=;i<m;i++){
cin>>u>>v>>w;
w=log(w)/log();
tem.to=v;
tem.w=w;
sm[u].push_back(tem);
}
dij(s);
if(dis[t]==-){
cout<<"-1"<<endl;
}
else{
//cout<<dis[t]<<endl;
ll tem=;
while(dis[t]!=){
if(dis[t]%==){
ans=(ans*tem)%mod;
}
dis[t]/=;
tem=(tem*tem)%mod;
}
cout<<ans<<endl;
}
return ;
}
Dijkstra的优先队列的更多相关文章
- Dijkstra算法优先队列实现与Bellman_Ford队列实现的理解
/* Dijkstra算法用优先队列来实现,实现了每一条边最多遍历一次. 要知道,我们从队列头部找到的都是到 已经"建好树"的最短距离以及该节点编号, 并由该节点去更新 树根 到其 ...
- dijkstra 的优先队列优化
既然要学习算法,就要学习到它的精髓,才能够使用起来得心应手. 我还是远远不够啊. 早就知道,dijkstra 算法可以用优先队列优化,我却一直不知道该怎样优化.当时,我的思路是这样的:假设有n个顶点, ...
- 小木乃伊到我家 dijkstra + 链表 + 优先队列
https://ac.nowcoder.com/acm/contest/96/E?&headNav=www&headNav=acm 题目描述 AA的欧尼酱qwb是个考古学家,有一天 ...
- dijkstra算法优先队列
d[i] 是起点到 I 节点的最短距离 void Dijkstra(int s) { priority_queue<P, vector<P>, greater<P> &g ...
- 隐式Dijkstra:在状态集合中用优先队列求前k小
这种技巧是挺久以前接触的了,最近又突然遇到几道新题,于是总结了一下体会. 这种算法适用的前提是,标题所述的"状态集合"大到不可枚举(否则枚举就行了qaq) ...
- poj 1511-- Invitation Cards (dijkstra+优先队列)
刚开始想复杂了,一直做不出来,,,其实就是两遍dijkstra+优先队列(其实就是板子题,只要能有个好的板子,剩下的都不是事),做出来感觉好简单...... 题意:有n个车站和n个志愿者,早上每个志愿 ...
- POJ 2387 Til the Cows Come Home (最短路 dijkstra)
Til the Cows Come Home 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Bessi ...
- ZOJ 3946 Highway Project(Dijkstra)
Highway Project Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, the emperor of the Marjar ...
- POJ 3037 Skiing(Dijkstra)
Skiing Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4668 Accepted: 1242 Special ...
随机推荐
- BUUCTF--xor
测试文件:https://buuoj.cn/files/caa0fdad8f67a3115e11dc722bb9bba7/7ea34089-68ff-4bb7-8e96-92094285dfe9.zi ...
- ubuntu 系统类似QQ截图工具:DeepinScrot,flameshot
经过一番探索! Ubuntu16.04 就用DeepinScrot 好用!不支持flameshot,反正我是半天没装成功 教程:https://blog.csdn.net/qq_19339041/ar ...
- 理解Thread.sleep()函数
转载自:http://www.cnblogs.com/ILove/archive/2008/04/07/1140419.html 我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时间 ...
- 安装Sublime Text 3 的过程
在Sublime Text官网下载最新的APP https://www.sublimetext.com 我下载的是 3143 安装之后开始在网上找注册码 我用了成功的注册码的是 这个应该是单个用户的, ...
- [Linux]Centos7/Centos6更改系统语言
Centos7系统语言配置信息保存在/etc/locale.conf文件内 更改步骤如下: 1.使用vim打开locale.conf文件 vim /etc/locale.conf2.编辑locale. ...
- USACO2008 Jan 电话网络
Time Limit: 10 Sec Memory Limit: 162 MB Description Farmer John决定为他的所有奶牛都配备手机,以此鼓励她们互相交流.不过,为此FJ必须在奶 ...
- 前端每日实战:56# 视频演示如何用纯 CSS 描述程序员的生活
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/YvYVvY 可交互视频 此视频是可 ...
- golang对象
对象和组合 package main import ( "fmt" ) type father struct { name string sex int } type sun st ...
- 带有IBM大脑的浮动机器人被成功引导至太空
近日,带有IBM大脑的浮动机器人被成功引导至太空,在接下来的装运前往国际空间站包近三吨的研究和再补给材料. 机器人的全名是CrewInteractiveMobileCompanion:Cimon.它看 ...
- Ubuntu安装openmpi
Ubuntu 下安装 openmpi 需要同时安装下面三个包: sudo apt-get install openmpi-bin openmpi-doc libopenmpi-dev 建议更新系统源之 ...