zoukankan      html  css  js  c++  java
  • HackerRank "Dijkstra: Shortest Reach 2"

    No big difference with "Breadth First Search: Shortest Reach", but this statement is crucial:

    If there are edges between the same pair of nodes with different weights, they are to be considered as is, like multiple edges.

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <unordered_map>
    #include <unordered_set>
    using namespace std;
    
    typedef pair<int, long> Node; // index - dist
    const long DIST_MAX = std::numeric_limits<long>::max();
    
    vector<long> dist;
    struct Comp
    {
        int operator() (const Node &n1, const Node &n2)
        {
            return n1.second > n2.second;
        }
    };
    int main() 
    {
        int t; cin >> t;
        while (t--)
        {
            unordered_map<int, unordered_map<int, int>> g;
    
            int n, m; cin >> n >> m;
            int mm = m;
            while (mm--){
                int x, y, r;
                cin >> x >> y >> r;
                x--, y--;
                if (g[x].find(y) == g[x].end())
                    g[x][y] = r;
                else
                    g[x][y] = std::min(g[x][y], r);
                if (g[y].find(x) == g[y].end())
                    g[y][x] = r;
                else
                    g[y][x] = std::min(g[y][x], r);
            }
            int s; cin >> s; s--;
            
            //
            dist.assign(n, DIST_MAX);
            dist[s] = 0;
    
            // inx
            priority_queue<Node, vector<Node>, Comp> hp;
            hp.push(Node(s, dist[s]));
            while (!hp.empty())
            {
                Node n = hp.top(); hp.pop();
                for (auto &c : g[n.first])
                {
                    int j = c.first;
                    int d = c.second;
                    if (dist[j] == DIST_MAX || (dist[n.first] + d) < dist[j])
                    {
                        dist[j] = dist[n.first] + d;
                        hp.push(Node(j, dist[j]));
                    }
                }
            }
    
            //    output
            for (int i = 0; i < n; i++)
                if (i != s)
                    cout << (dist[i] == DIST_MAX ? -1 : dist[i]) << " ";
            cout << endl;
        }
        return 0;
    }
    View Code

     

  • 相关阅读:
    全面了解 NOSQL
    金融业容灾技术分析
    银行业务知识(转)
    结合工作的业务连续性实践
    金融企业架构
    window 下拉取github项目失败 (Permission denied (publickey))
    vsftpd 配置文件
    nginx下配置虚拟主机
    linux 下安装ftp 并远程连接
    find_in_set
  • 原文地址:https://www.cnblogs.com/tonix/p/4695821.html
Copyright © 2011-2022 走看看