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; }
  • 相关阅读:
    updatepanel中不能使用fileupload的弥补方法
    AJAXPro用法,关于JS同步和异步调用后台代码的学习
    How do I get data from a data table in javascript?
    记不住ASP.NET页面生命周期的苦恼
    浅谈ASP.NET中render方法
    解决AjaxPro2中core.ashx 407缺少对象的问题
    ServU 6.0出来了
    关于X Server/Client和RDP的畅想
    这个Blog好像没有分页功能嘛
    AOC的显示器太烂了
  • 原文地址:https://www.cnblogs.com/zhanghaijie/p/10496653.html
Copyright © 2011-2022 走看看