ダイクストラの最短経路アルゴリズムテンプレート

// 
//   ダイクストラ妯℃澘た.cpp
 //   アルゴリズム
 // 
//   2018年8月6日にdavid.xuによって作成されます。
//   著作権漏2018骞?david.xu。全著作権所有。
// 
// 不能有负边权!
する#include <stdio.hに> 
する#include <cstdlib> 
の#include <CStringの> 
する#include <cmath> 
の#include <iostreamの> 
する#include <アルゴリズム> 
の#include <キュー> 
の#include <ベクトル>
 の#define MAXN 100010
 の#define PA対<int型、int型> 使用して名前空間はstdを、int型、N、M、S、[MAXN] DIS。


 

BOOL VIS [MAXN]。

PRIORITY_QUEUE <PA、ベクトル<PA>、大きな<PA>> Q。

構造体の縁{
     int型のValへ; 
}。
ベクター <エッジ> E [MAXN]。

ボイドダイクストラ()// DIS [i]が表示从起点到I的最短距离
{
     ためint型 i = 1 ; iが<= N; iは++ 
        DIS [I] = 十億一
    DIS [S] = 0 ; 
    q.push(make_pair(0 、S))。
    一方、(!q.empty())
    { 
        int型のx = q.top()は、第2。
        q.pop(); 
        もし(VIS [X])
             続けます
        VIS [X] = 1 以下のためにint型 i = 0 ; iは、Eを<[X] .size(); iは++ 
        { 
            int型 = Y E [X] [I] .TO。
            もし(DIS [X] + E [X] [I] .val < DIS [Y])
            { 
                DIS [Y] = DIS [X] + E [X] [I] .val。
                q.push(make_pair(DIS [Y]、Y))。
            } 
        } 
    } 
} 

int型のmain()
{ 
    scanf関数(" %D%D%D "、&​​N、&M&S)。
    以下のためにint型 I = 1 ; I <= M; iは++ 
    { 
        int型のX、Y、Z。
        scanf関数(" %D%D%D "、およびX&Y、およびZ)。
        エッジTMP。
        tmp.to = Y。
        tmp.val = Z。
        E [X] .push_back(TMP)。
    } 
    ダイクストラ()。
    int型 i = 1 ; iが<= N iが++ 
        のprintf(" %dの" 、DIS [I])。
    リターン 0 ; 
}

 

おすすめ

転載: www.cnblogs.com/Tidoblogs/p/11222313.html
おすすめ