算法总结-Dijkstra

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LZUwujiajun/article/details/82627146

邻接矩阵

#include<iostream>
using namespace std;

const int MAXV = 1000;
const int INF = 1e7;

int n,G[MAXV][MAXV],vis[MAXV] = {0},d[MAXV];

void dij(int s){
    fill(d,d+MAXV,INF);
    d[s] = 0;

    for(int i = 0;i < n;i++){
        int u = -1,MIN = INF;
        for(int j = 0j < n;j++){
            if(d[j] < MIN && vis[j] == 0){
                u = j;
                MIN = d[j];
            }
        }

        if(u == -1) return;
        vis[u] = 1;

        for(int v = 0;v < n;v++){
            if(G[u][v] != INF && vis[v] == 0 && d[u]+G[u][v] < d[v]){
                d[v] = d[u]+G[u][v];
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/LZUwujiajun/article/details/82627146