zoukankan      html  css  js  c++  java
  • 【Dijkstra】

    【摘自】:华山大师兄,推荐他的过程动画~

           myth_HG

    定义

    Dijkstra算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径。主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止。Dijkstra算法是很有代表性的最短路径算法,在很多专业课程中都作为基本内容有详细的介绍,如数据结构,图论,运筹学等等。注意该算法要求图中不存在负权边

    问题描述:在无向图 G=(V,E) 中,假设每条边 E[i] 的长度为 w[i],找到由顶点 V0 到其余各点的最短路径。(单源最短路径)

    算法描述

    1)算法思想:设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合(用S表示,初始时S中只有一个源点,以后每求得一条最短路径 , 就将加入到集合S中,直到全部顶点都加入到S中,算法就结束了),第二组为其余未确定最短路径的顶点集合(用U表示),按最短路径长度的递增次序依次把第二组的顶点加入S中。在加入的过程中,总保持从源点v到S中各顶点的最短路径长度不大于从源点v到U中任何顶点的最短路径长度。此外,每个顶点对应一个距离,S中的顶点的距离就是从v到此顶点的最短路径长度,U中的顶点的距离,是从v到此顶点只包括S中的顶点为中间顶点的当前最短路径长度。

    算法步骤

    a.初始时,S只包含源点,即S={v},v的距离为0。U包含除v外的其他顶点,即:U={其余顶点},若v与U中顶点u有边,则<u,v>正常有权值,若u不是v的出边邻接点,则<u,v>权值为∞。

    b.从U中选取一个距离v最小的顶点k,把k,加入S中(该选定的距离就是v到k的最短路径长度)。

    c.以k为新考虑的中间点,修改U中各顶点的距离;若从源点v到顶点u的距离(经过顶点k)比原来距离(不经过顶点k)短,则修改顶点u的距离值,修改后的距离值的顶点k的距离加上边上的权。

    d.重复步骤b和c直到所有顶点都包含在S中。

     

    实例

    对下图中的有向图,应用Dijkstra算法计算从源顶点1到其它顶点间最短路径的过程列在下表中。

    模板

      1 #include <iostream>
      2 #include <cstring>
      3 #include <cstdlib>
      4 #include <cstdio>
      5 #include <queue>
      6 #include <vector>
      7 #include <algorithm>
      8 using namespace std;
      9 const int MAX_V = 505;
     10 const int inf = 0x3f3f3f3f;
     11 
     12 typedef pair<int, int> pii;
     13 struct Dijkstra                              //封装dijkstra算法
     14 {
     15     struct Edge                       //定义边的结构体
     16     {
     17         int from, to, cost;
     18         Edge() {}
     19         Edge(int a, int b, int c) : from(a), to(b), cost(c) {}
     20     };
     21     int n, m;
     22     vector<Edge> G[MAX_V];             //边集合
     23     bool vis[MAX_V];
     24     int d[MAX_V];
     25     int prev[MAX_V];
     26     void init(int n)                //初始化,清空邻接表和边集合
     27     {
     28         this->n = n;
     29         for(int i = 0; i <= n; i++)
     30         {
     31             G[i].clear();
     32         }
     33     }
     34     void add(int from, int to, int dist)     //建图
     35     {
     36         G[from].push_back(Edge(from,to,dist));
     37     }
     38     void dijkstra(int s)
     39     {
     40         priority_queue<pii, vector<pii>, greater<pii> > q;
     41         memset(d, inf, sizeof(d));
     42         memset(prev, -1, sizeof(prev));
     43         d[s] = 0;
     44         q.push(pii(0, s));
     45 
     46         while(!q.empty())
     47         {
     48             pii p = q.top();    q.pop();
     49             int v = p.second;
     50             if(d[v] < p.first) continue;
     51             for(int i = 0; i < G[v].size(); i++)
     52             {
     53                 Edge e = G[v][i];
     54                 if(d[e.to] > d[e.from]+e.cost)
     55                 {
     56                     d[e.to] = d[e.from]+e.cost;
     57                     q.push(pii(d[e.to], e.to));
     58                     prev[e.to] = e.from;
     59                 }
     60             }
     61         }
     62     }
     63     vector<int> Get_Path(int t)
     64     {
     65         vector<int> path;
     66         for(; t != -1; t = prev[t])
     67             path.push_back(t);
     68         reverse(path.begin(), path.end());
     69         return path;
     70     }
     71 } D;
     72 
     73 
     74 int main()
     75 {
     76     int T;
     77     scanf("%d", &T);
     78     for(int kase = 1; kase <= T; kase++)
     79     {
     80         int V, E;
     81         scanf("%d%d", &V, &E);
     82         D.init(V);
     83         for(int i = 0; i < E; i++)
     84         {
     85             int u, v, w;
     86             scanf("%d%d%d", &u, &v, &w);
     87             D.add(u, v, w);     D.add(v, u, w);
     88         }
     89         int t; scanf("%d", &t);
     90         D.dijkstra(t);
     91         printf("Case %d:
    ", kase);
     92         vector<int> path = D.Get_Path(0);
     93 
     94         for(int i = 0; i < V; i++)
     95         {
     96             if(D.d[i] >= inf)
     97                 printf("Impossible
    ");
     98             else
     99                 printf("%d
    ", D.d[i]);
    100         }
    101     }
    102     return 0;
    103 }
    封装到class中

    STL priority_queue实现:复杂度O(|E|·log|V|)

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdlib>
     4 #include <cstdio>
     5 #include <queue>
     6 #include <vector>
     7 #include <algorithm>
     8 using namespace std;
     9 const int MAX_V = 505;
    10 const int inf = 0x3f3f3f3f;
    11 int d[MAX_V];
    12 int used[MAX_V];
    13 int cost[MAX_V][MAX_V];
    14 int prev[MAX_V];
    15 int V, E;
    16 
    17 void Dijkstra(int s) //Source is the source,M is the number of point;
    18 {
    19     memset(used, 0, sizeof(used));
    20     memset(prev, -1, sizeof(prev));
    21     memset(d, inf, sizeof(d));
    22     d[s] = 0;
    23 
    24     while(true)
    25     {
    26         int v = -1;
    27         for(int u = 0; u < V; u++)
    28         {
    29             if(!used[u] && (v == -1 || d[u] < d[v])) v = u;
    30         }
    31         if(v == -1) break;
    32         used[v] = true;
    33         for(int u = 0; u < V; u++)
    34         {
    35             int tmp = max(d[u], cost[v][u]);
    36             d[u] = max(d[u], cost[v][u]);
    37             prev[u] = v;
    38         }
    39     }
    40 }
    41 
    42 vector<int> Get_Path(int t)
    43 {
    44     vector<int> path;
    45     for(; t != -1; t = prev[t])
    46         path.push_back(t);
    47     reverse(path.begin(), path.end());
    48     return path;
    49 }
    邻接矩阵

    邻接矩阵实现:复杂度O(|V|2

  • 相关阅读:
    centos 7.1开机/etc/rc.local脚本不执行的问题
    ssh免密码登录之ssh-keygen的用法
    Centos 7.x临时的网络与路由配置
    Centos 7.x系统安装后的初始化配置
    U盘安装Centos7.1操作系统的问题记录
    linux系统中关于shell变量$*与$@的区别
    linux服务器init 5启动图形界面,报错Retrigger failed udev events
    rpmdb: unable to join the environment的问题解决
    BFS 典型的迷宫问题
    JUnit的基本使用
  • 原文地址:https://www.cnblogs.com/LLGemini/p/4737682.html
Copyright © 2011-2022 走看看