最短路 Dijkstra算法

Dijksitra算法求最短路仅仅适用于不存在右边是负权的情况(Bellman-Ford算法没有这一个限制)。主要特点是从起点为中心向外层层扩展,直到扩展到终点为止。

最短路的最优子结构性质

即一个最短路路径中经过的所有点这条路均是其最短路。(反证法易证)

Dijkstra基本思路:

①找到最短距离已经确定的顶点,从它出发更新相邻顶点的最短距离

②此后不需要再关心1中的”最短距离已经确定的顶点”

在最开始的时候,只有起点的最短距离是确定的。而在尚未使用过的顶点中,距离d[i]最小的顶点就是最短距离已经确定的顶点。由于不存在负边,所以d[i]不会再之后的更新中变小。这就是Dijkstra算法。

未优化的Dijkstra算法代码

int cost[max_v][max_v]; //使用邻接矩阵储存边(不存在就是INF)
int d[max_v]; //最短距离
bool used[max_v]; //已经确定最短路的图
int V;

//Dijkstra算法
void dijkstra(int s)
{
    //初始化
    fill(d,d+V,INF);
    fill(used,used+V,false);
    d[s] = 0;

    //最短路
    while(true) {
        int v = -1;
        for(int i = 0 ; i < V ; i ++) {
            if(!used[i]) {
                if(v == -1 || d[i] < d[v]) v = i;
            }
        }
        if(v == -1) break;//如果都确定了就退出
        used[v] = true;
        for(int i = 0 ; i < V ; i ++) {
            d[i] = min(d[i],d[v]+cost[v][i]);
        }
    }
}

使用优先队列优化代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct edge{int to,cost;};
typedef pair<int,int> P; //first是最短距离 second是顶点编号
int V;
vector<edge> G[max_v];//邻接表
int d[max_v];
void dijkstra(int s)
{
    //通过指定greater<P>参数,堆按照first从小到大的顺序取出值
    priority_queue<P,vector<P>,greater<P>> que;
    fill(d,d+V,INF);
    d[s] = 0;
    que.push(P(0,s));
    while(!que.empty()) {
        P p = que.top();
        que.pop();
        int v = p.second;//编号
        if(d[v] < p.first) continue;
        for(int i = 0 ; i < G[v].size() ; i ++) {
            edge e = G[v][i];
            if(d[e.to] > d[v]+e.cost) {
                d[e.to] = d[v]+e.cost;
                que.push(P(d[e.to],e.to));
            }
        }
    }
}

Dijkstra算法的复杂度是O(|E|log|V|),可以更加高效求解最短路。但如果有负边还是要用Bellman-Ford算法。

时间: 2024-07-30 13:30:23

最短路 Dijkstra算法的相关文章

POJ-3268-最短路(dijkstra算法)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12494   Accepted: 5568 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X

ACM: HDU 2544 最短路-Dijkstra算法

HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗? Input 输入包括多组数据.每组数据第一行是两个整数N.M(N<=100,M<

单源最短路Dijkstra算法——matlab实现

迪杰斯特拉(Dijkstra)算法是典型最短路径算法,用于计算一个节点到其他节点的最短路径. 它的主要特点是以起始点为中心向外层层扩展(广度优先搜索思想),直到扩展到终点为止. 基本思想 通过Dijkstra计算图G中的最短路径时,需要指定起点s(即从顶点s开始计算). 此外,引进两个集合S和U.S的作用是记录已求出最短路径的顶点(以及相应的最短路径长度),而U则是记录还未求出最短路径的顶点(以及该顶点到起点s的距离). 初始时,S中只有起点s:U中是除s之外的顶点,并且U中顶点的路径是"起点s

hdu2544 最短路 Dijkstra算法

最短路(Dijkstra算法模板题) Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 96778    Accepted Submission(s): 41849借鉴链接:https://blog.csdn.net/UncleJokerly/article/details/79703622 Problem Description 在每年的

POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects

六度分离(floyd算法,SPFA算法,最短路—Dijkstra算法)

Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 36   Accepted Submission(s) : 16 Problem Description 1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为"小世界现象(small world phenomenon)"的著名假说.大意是说.不论什么2个素不相识的人中间最多

matlab 单元最短路 Dijkstra算法 有向图 无向图

W = [2 8 1 1 6 5 1 2 3 6 4 9 3 7 9]; S = [0 0 0 1 1 3 3 3 5 5 6 4 6 2 2];S=S+1; T = [1 3 2 4 3 4 5 6 4 7 5 7 7 3 6];T=T+1; IDS={'u0','u1','u2','u3','u4','u5','u6','u7'}; DG = sparse(S,T,W,8,8)%求稀疏矩阵 UG =tril(DG+DG')%取矩阵和转置矩阵和的下三角矩阵. bg=biograph(DG,ID

单源最短路-dijkstra算法(未优化)

1 bool used[maxn]; 2 int g[maxn][maxn]; // 边未联系的填充为INF 3 int d[maxn]; 4 void dijkstra(int s){ 5 memset(g,false,sizeof(g)); 6 memset(d,INF,sizeof(d)); 7 d[s] = 0; 8 while(1){ 9 int v = -1; 10 for(int u = 0; u<v; u++){ 11 if(!used[u] && (v == -1|

hubust 1339Touring (最短路Dijkstra算法)

Description: The best friends Mr. Li and Mr. Liu are touring in beautiful country M. M has n cities and m two-way roads in total. Each road connects two cities with fixed length.We assume that the cost of car traveling on the road is only related to