Shortest path (Dijkstra) algorithm

The second shortest path algorithm:

Dijkdtra algorithm template:

Using a critical matrix, its time complexity is O(nlogn):

Template topic link: http://acm.hdu.edu.cn/showproblem.php?pid=1874


Problem Description
A province has finally built a lot of roads after implementing a smooth project plan for many years. But more roads are not good either. Every time you want to get from one town to another, there are many road options to choose from, some of which travel much shorter distances than others. This bothers pedestrians.

Now that you know the starting point and ending point, please calculate the shortest distance you need to walk from the starting point to the ending point.
 

Input
This topic contains multiple sets of data, please process until the end of the file.
The first row of each set of data contains two positive integers N and M (0<N<200, 0<M<1000), which represent the number of existing towns and the number of roads that have been built, respectively. Towns are numbered from 0 to N-1.
Next is M lines of road information. Each line has three integers A, B, X (0<=A, B<N, A!=B, 0<X<10000), indicating that there is a two-way road of length X between town A and town B.
Then there are two integers S, T (0<=S,T<N) in the next line, representing the starting point and the ending point respectively.
 

Output
For each set of data, output the shortest distance to travel on one line. If there is no route from S to T, output -1.
 

Sample Input
 
  
3 3 0 1 1 0 2 3 1 2 1 0 2 3 1 0 1 1 1 2
 

Sample Output
 
  
2 -1


     The code is as follows (template):

            

#include <iostream>
using namespace std;
const int maxn  = 210;
const int inf = 0xfffff;

int n, m ,x, y;
int map[maxn][maxn], dis[maxn], vis[maxn];

void Dijkstra (int res) {
    for (int i = 0; i < n; i++){
        dis[i] = map[res][i];
        show [i] = 0;
    }
    dis[res] = 0;
    vis [res] = 1;
    int temp, k ;
    for (int i = 0; i < n; i++){
        temp = inf;
        for (int j = 0; j < n; j++){
            if (!vis[j] && temp > dis[j]){
                k = j;
                temp = dis[j];
            }
        }
        /*if (temp == inf){
            break;
        }*/
        vis [k] = 1;
        for (int j = 0; j < n; j++){
            if (!vis[j] && dis[j] > dis[k] + map[k][j]){
                dis[j] = dis[k] + map[k][j];
            }
        }
    }
}

int main(){
    while (cin >> n >> m){
        for (int i = 0; i < n; i++){
            for (int j = 0; j < n; j++){
                if (i == j){
                    map[i][j] = 0;
                }
                else{
                    map[i][j] = inf ;
                }
            }
        }
        int a, b, c ;
        for (int i = 0; i < m; i++){
            cin >> a >> b >> c;
            if (map[a][b] > c){
                map[a][b] = map[b][a] = c ;
            }
        }
        cin >> x >> y;
        Dijkstra (x);
        if (dis[y] < inf){
            cout << dis[y] << endl ;
        }
        else{
            cout << -1 << endl;
        }
    }
    return 0;
}
 
 

            Continuing to learn other algorithms... To be continued...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325885296&siteId=291194637