Dijskstra算法
采用优先队列优化
#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算法的更多相关文章
- 最短路径算法(I)
弗洛伊德算法(Floyed-Warshall) 适用范围及时间复杂度 该算法的时间复杂度为O(N^3),适用于出现负边权的情况. 可以求取最短路径或判断路径是否连通.可用于求最小环,比较两点之间的大小 ...
- 单源最短路径-Dijkstra算法
1.算法标签 贪心 2.算法描述 具体的算法描述网上有好多,我觉得莫过于直接wiki,只说明一些我之前比较迷惑的. 对于Dijkstra算法,最重要的是维护以下几个数据结构: 顶点集合S : 表示已经 ...
- 最短路径算法的实现(dijskstra):Python
dijskstra最短路径算法步骤: 输入:图G=(V(G),E(G))有一个源顶点S和一个汇顶点t,以及对所有的边ij属于E(G)的非负边长出cij. 输出:G从s到t的最短路径的长度. 第0步:从 ...
- 避免死锁的银行家算法C++程序实现
本篇博文为追忆以前写过的算法系列第二篇(20081021) 温故知新 目的:具有代表性的死锁避免算法是Dijskstra给出的银行家算法.本实验是基于银行家算法的思想通过编写C++程序实现银行家 ...
- 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 ...
- 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 ...
- B树——算法导论(25)
B树 1. 简介 在之前我们学习了红黑树,今天再学习一种树--B树.它与红黑树有许多类似的地方,比如都是平衡搜索树,但它们在功能和结构上却有较大的差别. 从功能上看,B树是为磁盘或其他存储设备设计的, ...
- 分布式系列文章——Paxos算法原理与推导
Paxos算法在分布式领域具有非常重要的地位.但是Paxos算法有两个比较明显的缺点:1.难以理解 2.工程实现更难. 网上有很多讲解Paxos算法的文章,但是质量参差不齐.看了很多关于Paxos的资 ...
- 【Machine Learning】KNN算法虹膜图片识别
K-近邻算法虹膜图片识别实战 作者:白宁超 2017年1月3日18:26:33 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...
随机推荐
- ip访问网站和localhost访问网站中top使用
对于相对定位,使用margin-top不用简单使用top. top在localhost中能正常显示,在ip访问时会出现多余空白. margin-top不管是localhost中还是ip中都能正常显示.
- 带你了解zabbix整合ELK收集系统异常日志触发告警~
今天来了解一下关于ELK的“L”-Logstash,没错,就是这个神奇小组件,我们都知道,它是ELK不可缺少的组件,完成了输入(input),过滤(fileter),output(输出)工作量,也是我 ...
- ELK 使用4-Kafka + zookpeer
一.zookpeer操作 1.登录 /application/elk/zookeeper/bin/zkCli.sh -server 127.0.0.1:2181 2.查看结构 ls / 上面的显示结果 ...
- 009 spring boot中文件的上传与下载
一:任务 1.任务 文件的上传 文件的下载 二:文件的上传 1.新建一个对象 FileInfo.java package com.cao.dto; public class FileInfo { pr ...
- 3dsmax不同版本 pyside qt UI 设置max窗口为父窗口的方法
3dsmax不同版本 pyside qt widget 设置 max 窗口为父窗口的方法 前言: 3dsmax 在 2014 extension 之后开始集成 Python 和 PySide,但是在版 ...
- mysql的基础增删改查(一)
修改,操作表:1.建表:create table MyClass(id int(4) not null primary key auto_increment,name char(20) not nul ...
- HDFS格式化namenode后启动集群datanode不启动
这是因为格式化namenode之后,namenode节点中VERSION的ClusterID会改变!!! 解决办法: 1.打开namenode节点的hdfs-site.xml文件,查看你的nameno ...
- eclipse tomcat报Several ports(8005 8080 8009)端口被占用问题解决方案
在启动tomcat的时候eclipse突然报错 Several ports (8005,8080,8009) required by Tomcat v6.0 Server at localhost a ...
- HDU 2586 How far away ?(经典)(RMQ + 在线ST+ Tarjan离线) 【LCA】
<题目链接> 题目大意:给你一棵带有边权的树,然后进行q次查询,每次查询输出指定两个节点之间的距离. 解题分析:本题有多重解决方法,首先,可用最短路轻易求解.若只用LCA解决本题,也有三种 ...
- Git rebase命令实战
一.前言 一句话,git rebase 可以帮助项目中的提交历史干净整洁!!! 二.避免合并出现分叉现象 git merge操作 1.新建一个 develop 分支 2.在develop分支上新建 ...