C++使用优先队列实现dijkstra

C++优先队列的dijkstra解法

我使用二维数组来存储结果,找到了所有点之间的最短路径

初始图:

A B C D
A 0 4 10 1
B 2 0 1 1
c 8 14 0 14
D 15 9 10 0

结果:

A B C D
A 0 4 5 1
B 2 0 1 1
c 8 12 0 9
D 11 9 10 0

标记数组:用来寻找最终的路径

A B C D
A A A B A
B B B B B
c C A C A
D B D D D
#include
#include
#include
#include
#include
using namespace std;

struct Edge{
    int weight=0;
    int end;
    bool friend operator>( Edge a, Edge b){
        return a.weight>b.weight;
}

};
//生成随机图
int Generate_Graph(vector<Edge>* &graph){

    int n;
    cout<<"Please enter the size of the graph:";
    cin>>n;
    graph = new vector<Edge>[n];
    srand(time(NULL));

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(i!=j){
                int weight = rand()%15+1;
                Edge e;
                e.end=j;
                e.weight = weight;
                graph[i].push_back(e);
            }
        }
    }

    for(int i=0;i<n;i++){
        cout<<char('A'+i);
        for(Edge e:graph[i]){
            cout.width(6);
            cout.setf(ios::right);
            cout<<char(e.end+'A')<<":"<<e.weight;
        }
        cout<<'\n'<<'\n';
    }
    return n;
}
//算法的开始
void dijkstra1(vector<Edge>* &graph,int size){
	//进行初始化值
    bool visit[size][size];
    int result[size][size];//to store the distance of any two node
    for(int i=0;i<size;i++){
        for(int j=0;j<size;j++){
            result[i][j] = 100000;
        }
    }
    int tag[size][size];//trace back to find the complete result.

    double tic = clock();//计时
    //对于每一个节点,我都算出他和其他节点之间的最短距离,并且做标记,tag是标记数组
    for(int i=0;i<size;i++){// for each node ,I find the shortest path between this node to the other nodes.

        priority_queue<Edge,vector<Edge>,greater<Edge>> q;//the priority queue to store node
        int start = i;
        visit[start][start] = true;
        result[start][start] = 0;
        tag[start][start]=start; //
        for(Edge e:graph[start]){
            q.push(e);
            result[start][e.end]=e.weight;
            tag[start][e.end]=start;
        }
        int j=0;
        while(j<size&&!q.empty()){
            Edge e = q.top();

            q.pop();
            visit[start][e.end]=true;
            for(Edge ee:graph[e.end]){

                q.push(ee);
                int a =result[start][e.end]+ee.weight;
                if(result[start][ee.end]>a){
                    result[start][ee.end] = a;
                    tag[start][ee.end]=e.end;
                }
            }
            j++;

        }

        //打印最终结果  
        cout<<' ';
        for(int k=0;k<size;k++){
            cout.setf(ios::left);
            cout.width(8);
            cout<<char('A'+k);
        }
        cout<<endl;
        cout<<char('A'+i);
        for(int k=0;k<size;k++){
            cout.setf(ios::left);
            cout.width(8);
            cout<<result[start][k];

        }
        cout<<endl;

        cout<<' ';
        for(int k=0;k<size;k++){
            cout.setf(ios::left);
            cout.width(8);
            cout<<char(tag[start][k]+65);

        }
        cout<<endl;
        cout<<endl;



    }
    double toc = clock();
    //cout<<(toc-tic)/CLOCKS_PER_SEC<

}


int main(){
    vector<Edge> *graph;
    int n = Generate_Graph(graph);

    dijkstra1(graph,n);


}


你可能感兴趣的:(C++)