zoukankan      html  css  js  c++  java
  • 882. Reachable Nodes In Subdivided Graph

    Starting with an undirected graph (the "original graph") with nodes from 0 to N-1, subdivisions are made to some of the edges.

    The graph is given as follows: edges[k] is a list of integer pairs (i, j, n) such that (i, j) is an edge of the original graph,

    and n is the total number of new nodes on that edge. 

    Then, the edge (i, j) is deleted from the original graph, n new nodes (x_1, x_2, ..., x_n) are added to the original graph,

    and n+1 new edges (i, x_1), (x_1, x_2), (x_2, x_3), ..., (x_{n-1}, x_n), (x_n, j) are added to the original graph.

    Now, you start at node 0 from the original graph, and in each move, you travel along one edge. 

    Return how many nodes you can reach in at most M moves.

    Example 1:

    Input: edges = [[0,1,10],[0,2,1],[1,2,2]], M = 6, N = 3
    Output: 13
    Explanation: 
    The nodes that are reachable in the final graph after M = 6 moves are indicated below.
    
    

    Example 2:

    Input: edges = [[0,1,4],[1,2,6],[0,2,8],[1,3,1]], M = 10, N = 4
    Output: 23

    Note:

    1. 0 <= edges.length <= 10000
    2. 0 <= edges[i][0] < edges[i][1] < N
    3. There does not exist any i != j for which edges[i][0] == edges[j][0] and edges[i][1] == edges[j][1].
    4. The original graph has no parallel edges.
    5. 0 <= edges[i][2] <= 10000
    6. 0 <= M <= 10^9
    7. 1 <= N <= 3000
    8. A reachable node is a node that can be travelled to using at most M moves starting from node 0.

    Approach #1: C++. [graph/heap]

    class Solution {
    public:
        int reachableNodes(vector<vector<int>>& edges, int M, int N) {
            unordered_map<int, unordered_map<int, int>> g;
            for (const auto& edge : edges) {
                g[edge[0]][edge[1]] = g[edge[1]][edge[0]] = edge[2];
            }
            
            priority_queue<pair<int, int>> q;  // HP : node
            unordered_map<int, int> HP;  // node : HP
            
            q.push({M, 0});
            
            while (!q.empty()) {
                int hp = q.top().first;
                int cur = q.top().second;
                q.pop();
                if (HP.count(cur)) continue;
                HP[cur] = hp;
                for (const auto& pair : g[cur]) {
                    int nxt = pair.first;
                    int nxt_hp = hp - pair.second - 1;
                    if (HP.count(nxt) || nxt_hp < 0) continue;
                    q.push({nxt_hp, nxt});
                }
            }
            
            int ans = HP.size();
            for (const auto& e : edges) {
                int uv = HP.count(e[0]) ? HP[e[0]] : 0;
                int vu = HP.count(e[1]) ? HP[e[1]] : 0;
                ans += min(e[2], uv + vu);
            }
            
            return ans;
        }
    };
    

      

    Analysis:

    https://zxi.mytechroad.com/blog/graph/leetcode-882-reachable-nodes-in-subdivided-graph/

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    SVNKit学习——基于Repository的操作之print repository tree、file content、repository history(四)
    java操作svn【svnkit】实操
    python笔记38-使用zmail发各种邮件案例代码
    python笔记37-史上最好用的发邮件zmail
    python笔记3-邮件发送(smtplib)
    第9期《python3接口自动化测试》课程,6月29号开学!
    anyproxy学习4-Linux(Centos)搭建anyproxy环境
    anyproxy学习3-修改返回内容(beforeSendResponse)
    anyproxy学习2-rule模块实现接口mock功能
    anyproxy学习1-windows平台安装和抓手机app上https请求
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10200904.html
Copyright © 2011-2022 走看看