采用优先队列优化

#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. Windows 7 编译64位boost库

    1. 官网下载boost 2. 解压boost到D:\Library\boost_1_64_0 3. 打开 VS2015 x64 本机工具命令提示符,输入 "d:" 回车,再输入 ...

  2. JS如何监听动画结束

    场景描述 在使用JS控制动画时一般需要在动画结束后执行回调去进行DOM的相关操作,所以需要监听动画结束进行回调.JS提供了以下事件用于监听动画的结束,简单总结学习下. CSS3动画监听事件 trans ...

  3. JAVA连接MYSQL,查询 ,添加,删除,语句

        package com; import Java.sql.*;/** *//** * @author Administrator */public class ggg {    private ...

  4. POJ3740 Easy Finding 舞蹈链 DLX

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目 精确覆盖问题模板题 算法 DLX算法 学习DLX算法--传送门 代码 #include <cstring> ...

  5. Orleans2.0正式版发布

    2.0版本支持.net core. Orleans是一个微软研究创建的用于大规模分布式计算的框架,使用者不需要学习和应用复杂的并发或其他缩放模式. 根据我几个项目的使用经验来说,Orleans虽然有应 ...

  6. 008 在大数据中,关于native包的编译步骤

    一.问题的由来: 二.解决问题的方法(所有的操作在root下完成): 1.前期需要的环境,下面的已经在伪分布式中配置好,不再重复 配置好jdk 配置好hadoop 2.上传还需要包 apache-ma ...

  7. day64 django django零碎知识点整理

    本文转载自紫金葫芦,哪吒,liwenzhou.cnblog博客地址 简单了解mvc框架和MTV框架, mvc是一种简单的软件架构模式: m----model,模型 v---view,视图 c---co ...

  8. 无向图的 DFS 和 BFS实现 (以邻接表存储的图)

    #include <iostream> #include <queue> using namespace std; #define MaxVertexNum 10 typede ...

  9. java的conllections.sort排序

    https://www.cnblogs.com/yw0219/p/7222108.html?utm_source=itdadao&utm_medium=referral

  10. [ 高危 ] my网任意账户登陆

    该网站的任意登录其实都已经提交得差不多了,本来以为这个漏洞会是一个重复的,然而试了一下发现思路奇葩. 任意登录,一般都为验证码爆破,4位手机验证码,而用于拦截的图片验证码没有或者可以重复使用,所以就能 ...