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 ...
随机推荐
- 如何用CSS定义一个动画?
<style type="text/css"> div{ width:100px;height: 100px; animation: carton 5s; backgr ...
- java随机数Math.random()
double random=Math.random();//返回[0,1)随机数 (int)(Math.random()*6)//返回0-5:随机数 (int)(Math.random()*6+1)/ ...
- elasticsearch 深入 —— Search After实时滚动查询
Search After 一般的分页需求我们可以使用form和size的方式实现,但是这种分页方式在深度分页的场景下应该是要避免使用的.深度分页会随着请求的页次增加,所消耗的内存和时间的增长也是成比例 ...
- ajax中的application/x-www-form-urlencoded中的使用[转]
一,HTTP上传的基本知识 在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型.下边是说明: application/x-w ...
- 一、免费API调用
一.免费API调用: 免费天气api接口 JS调用示例 <!DOCTYPE html> <html lang="zh-CN"> <head> & ...
- 自己关于SSM框架的搭建
第一步 导入相应的包 spring springmvc 需要的包 spring-webmvc spring-aop spring-beans apring-context spring-core sp ...
- hdu1214 圆桌会议
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1214 HDU ACM集训队的队员在暑假集训时经常要讨论自己在做题中遇到的问题.每当面临自己解决不了的问 ...
- Wait and Click Element
Wait and Click Element [Documentation] 等待元素出现并单击元素 [Arguments] ${locator} Wait Until Element Is Visi ...
- canvas 图片反色
代码实例: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <tit ...
- python基础:10.多线程装饰器模式下的单例模式
with def __enter__ def __close__ 闭包: 装饰器: 闭包的延迟绑定: 单例模式的应用: