采用优先队列优化

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+;
const int maxm=1e6+;
int head[maxn],ver[maxm],edge[maxm],nxt[maxm],d[maxn];
int tot;
int v[maxn];
int n,m;
const int INF=0x3f3f3f3f;
priority_queue<pair<int,int> > q;
void add(int x,int y,int z)
{
ver[++tot]=y;
edge[tot]=z;
nxt[tot]=head[x];
head[x]=tot;
}
void dij()
{
memset(d,INF,sizeof(d));
memset(v,,sizeof(v));
d[]=;
q.push(make_pair(,));
while(q.size())
{
int x=q.top().second;
q.pop();
if(v[x]) continue;
v[x]=;
for(int i=head[x]; i; i=nxt[i])
{
int y=ver[i];
int z=edge[i];
if(d[y]>d[x]+z)
{
d[y]=d[x]+z;
q.push(make_pair(-d[y],y));
}
}
}
} int main()
{
int n,m;
while(scanf("%d%d",&n,&m)&&n)
{
tot=;
memset(head,,sizeof(head));
for(int i=; i<=m; i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
dij();
printf("%d\n",d[n]);
}
}
//6 9
//1 2 1
//1 3 12
//2 3 9
//2 4 3
//3 5 5
//4 3 4
//4 5 13
//4 6 15
//5 6 4
//
//0 1 8 4 13 17
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int maxn = ;
const int INF = <<;
struct node
{
int x,d;
node() {}
node(int a,int b)
{
x=a;
d=b;
}
bool operator < (const node & a) const
{
if(d==a.d) return x<a.x; // 按d从小到大,x从大到小自动排序
else return d > a.d;
}
};
vector<node> eg[maxn];
int dis[maxn],n;
void Dijkstra(int s)
{
int i;
for(i=; i<=n; i++) dis[i]=INF;
dis[s]=;
//用优先队列优化,就是个堆
priority_queue<node> q;
q.push(node(s,dis[s]));
while(!q.empty())
{
node x=q.top();
q.pop();
for(i=; i<eg[x.x].size(); i++)
{
node y=eg[x.x][i];
if(dis[y.x]>x.d+y.d)
{
dis[y.x]=x.d+y.d;
q.push(node(y.x,dis[y.x]));
}
}
}
}
int main()
{
int a,b,d,m;
while(scanf("%d%d",&n,&m))
{
for(int i=; i<=n; i++) eg[i].clear();
while(m--)
{
scanf("%d%d%d",&a,&b,&d);
eg[a].push_back(node(b,d));
eg[b].push_back(node(a,d));
}
Dijkstra();
for(int i=; i<=n; i++)
printf("%d ", dis[i]);
}
return ;
}
//6 9
//1 2 1
//1 3 12
//2 3 9
//2 4 3
//3 5 5
//4 3 4
//4 5 13
//4 6 15
//5 6 4
//
//0 1 8 4 13 17

朴素版本,时间复杂度比上面的要高

#include<cstdio>
int e[][];
int dis[];
int book[];
int main()
{
int n, m;
int inf=;
scanf("%d%d", &n, &m);
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
if(i==j)
e[i][j]=;
else
e[i][j]=inf;
int t1, t2, t3;
for(int i=; i<=m; i++)
{
scanf("%d%d%d", &t1, &t2, &t3);
e[t1][t2]=t3;
}
for(int i=; i<=n; i++)
{
dis[i]=e[][i];
}
int u, min;
book[]=;
for(int i=; i<=n; i++)
{
min=inf;
for(int j=; j<=n; j++)
{
if(dis[j]<min && book[j]==)
{
u=j;
min=dis[j];
}
}
book[u]=;
for(int v=; v<=n; v++)
{
if(e[u][v]<inf && dis[v]>dis[u]+e[u][v])
{
dis[v]=dis[u]+e[u][v];
}
}
}
for(int i=; i<=n; i++)
printf("%d ", dis[i]);
}

Dijskstra算法的更多相关文章

  1. 最短路径算法(I)

    弗洛伊德算法(Floyed-Warshall) 适用范围及时间复杂度 该算法的时间复杂度为O(N^3),适用于出现负边权的情况. 可以求取最短路径或判断路径是否连通.可用于求最小环,比较两点之间的大小 ...

  2. 单源最短路径-Dijkstra算法

    1.算法标签 贪心 2.算法描述 具体的算法描述网上有好多,我觉得莫过于直接wiki,只说明一些我之前比较迷惑的. 对于Dijkstra算法,最重要的是维护以下几个数据结构: 顶点集合S : 表示已经 ...

  3. 最短路径算法的实现(dijskstra):Python

    dijskstra最短路径算法步骤: 输入:图G=(V(G),E(G))有一个源顶点S和一个汇顶点t,以及对所有的边ij属于E(G)的非负边长出cij. 输出:G从s到t的最短路径的长度. 第0步:从 ...

  4. 避免死锁的银行家算法C++程序实现

     本篇博文为追忆以前写过的算法系列第二篇(20081021) 温故知新 目的:具有代表性的死锁避免算法是Dijskstra给出的银行家算法.本实验是基于银行家算法的思想通过编写C++程序实现银行家 ...

  5. PAT Advanced 1030 Travel Plan (30) [Dijkstra算法 + DFS,最短路径,边权]

    题目 A traveler's map gives the distances between cities along the highways, together with the cost of ...

  6. PAT Advanced 1018 Public Bike Management (30) [Dijkstra算法 + DFS]

    题目 There is a public bike service in Hangzhou City which provides great convenience to the tourists ...

  7. B树——算法导论(25)

    B树 1. 简介 在之前我们学习了红黑树,今天再学习一种树--B树.它与红黑树有许多类似的地方,比如都是平衡搜索树,但它们在功能和结构上却有较大的差别. 从功能上看,B树是为磁盘或其他存储设备设计的, ...

  8. 分布式系列文章——Paxos算法原理与推导

    Paxos算法在分布式领域具有非常重要的地位.但是Paxos算法有两个比较明显的缺点:1.难以理解 2.工程实现更难. 网上有很多讲解Paxos算法的文章,但是质量参差不齐.看了很多关于Paxos的资 ...

  9. 【Machine Learning】KNN算法虹膜图片识别

    K-近邻算法虹膜图片识别实战 作者:白宁超 2017年1月3日18:26:33 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...

随机推荐

  1. libssl.so.10: cannot open shared object file: No such file or directory

    今天在使用yum安装东西的时候报错 yum list There was a problem importing one of the Python modulesrequired to run yu ...

  2. Codeforces 235C Cyclical Quest 字符串 SAM KMP

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF235C.html 题目传送门 -  CF235C 题意 给定一个字符串 $s$ ,多组询问,每组询问的形式为 ...

  3. Codeforces Gym100783H 最短路 其他

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF-Gym100783H.html 题目传送门 - CF-Gym100783H 题意 给定一个 $n$ 个节点 ...

  4. Codeforces 802I Fake News (hard) (SA+单调栈) 或 SAM

    原文链接http://www.cnblogs.com/zhouzhendong/p/9026184.html 题目传送门 - Codeforces 802I 题意 求一个串中,所有本质不同子串的出现次 ...

  5. Redis数据库 02事务| 持久化| 主从复制| 集群

    1. Redis事务 Redis不支持事务,此事务不是关系型数据库中的事务: Redis事务是一个单独的隔离操作:事务中的所有命令都会序列化.按顺序地执行.事务在执行的过程中,不会被其他客户端发送来的 ...

  6. utf-8和utf8的区别

    utf-8 和 utf8 的区别与使用: "UTF-8" 是标准写法,php 在 Windows 系统里的英文不区分大小写,所以也可以写成 "utf-8".&q ...

  7. day3逻辑运算符

    今天主讲逻辑运算符 以下是老师的大纲: # + - * / % ** // # == != <> # count = count + 1      count += 1 # count = ...

  8. jQuery获得页面绝对和相对的位置

    获得某一元素绝对x,y位置,可以用offset方法 var X = $('#DivID').offset().top; var y=$("#divid").offset().lef ...

  9. PostgreSQL 调用存储过程返回结果集

    创建返回结果集类型的存储过程: CREATE OR REPLACE FUNCTION public.f_get_member_info( id integer, productname charact ...

  10. ContextRefreshedEvent事件使用注意事项(Spring)

    0 概述ContextRefreshedEvent 事件会在Spring容器初始化完成会触发该事件.我们在实际工作也可以能会监听该事件去做一些事情,但是有时候使用不当也会带来一些问题. 1 防止重复触 ...