zoukankan      html  css  js  c++  java
  • 迪杰斯特拉+优先队列实现

    迪杰斯特拉算法是一种经典的图论算法,用于求非负带权图的最短路径,我通过使用c++ stl库中的优先队列

    priority_queue进行实现。

    #include<iostream>
    #include<map>
    #include<vector>
    #include<string.h>
    #include<queue>
    #include<unordered_set>
    #include<utility>
    #include<map>
    #include<unordered_map>
    /*int 代表距离,string代表顶点*/
    const int maxv=1000000007;
    using namespace std;
    
    void show(unordered_map<string,unordered_map<string,int>>&g)
    {
        for(auto&item:g)
        {
            cout<<item.first<<endl;
            for(auto&temp:item.second)
            {
                cout<<temp.first<<" "<<temp.second<<endl;
            }
        }
    }
    
    void init(unordered_map<string,unordered_map<string,int>>g,unordered_map<string,int>&distance,string& s)
    {
        for(auto&item:g)
        {
            if(item.first!=s)
                distance[item.first] = maxv;
            else
                distance[item.first] = 0;
        }
    }
    
    void dijkstra(unordered_map<string,unordered_map<string,int>>&graph,string s,unordered_map<string,int>&distance,unordered_map<string,string>&parent)
    {
        //初始化距离数组
        init(graph,distance,s);
        priority_queue<pair<int,string>,vector<pair<int,string>>,greater<pair<int,string>>>que;
        unordered_set<string> seen;//置访问标记
        que.push(pair<int,string>(0,s));//
        parent[s]="None";
        while(!que.empty())
        {
            pair<int,string> item = que.top();
            que.pop();
            int dist = item.first;
            string vertex = item.second;
            seen.insert(vertex);//从优先队列弹出后则到达这个点的路径已经确定
            for(auto&tmp:graph[vertex])
            {
                //如果这个顶点还没有添加的话
                if(seen.find(tmp.first)==seen.end())
                {
                    if(dist + graph[vertex][tmp.first] < distance[tmp.first])
                    {
                        distance[tmp.first] = dist + graph[vertex][tmp.first];
                        parent[tmp.first]=vertex;
                        que.push(pair<int,string>(distance[tmp.first],tmp.first));
                    }
                }
            }
        }
    }
    
    int main()
    {
        string node1,node2;
        int w;
       //图的数据结构 unordered_map
    <string,unordered_map<string,int>>graph; unordered_map<string,int>distance; unordered_map<string,string>parent; while(cin>>node1>>node2>>w) { graph[node1][node2]=w; } string s="A"; dijkstra(graph,s,distance,parent); for(auto&temp:distance) { cout<<temp.first<<" "<<temp.second<<endl; } cout<<"parent 数组"<<endl; for(auto&temp:parent) { cout<<temp.first<<" "<<temp.second<<endl; } show(graph); return 0; }
  • 相关阅读:
    每天拿出来2小时浪费(文/王路) 作者: 王路
    objective-c自学总结(二)---init/set/get方法
    objective-c自学总结(一)---面向对象
    水仙花数
    独木舟上的旅行
    阶乘之和
    小明的调查统计
    管闲事的小明
    重温《STL源码剖析》笔记 第一章
    重温《STL源码剖析》笔记 第三章
  • 原文地址:https://www.cnblogs.com/zhanghaijie/p/10496653.html
Copyright © 2011-2022 走看看