leetcode 882. Reachable Nodes In Subdivided Graph

You are given an undirected graph (the "original graph") with n nodes labeled from 0 to n - 1. You decide to subdivide each edge in the graph into a chain of nodes, with the number of new nodes varying between each edge.

The graph is given as a 2D array of edges where edges[i] = [ui, vi, cnti] indicates that there is an edge between nodes ui and vi in the original graph, and cnti is the total number of new nodes that you will subdivide the edge into. Note that cnti == 0 means you will not subdivide the edge.

To subdivide the edge [ui, vi], replace it with (cnti + 1) new edges and cnti new nodes. The new nodes are x1x2, ..., xcnti, and the new edges are [ui, x1][x1, x2][x2, x3], ..., [xcnti-1, xcnti][xcnti, vi].

In this new graph, you want to know how many nodes are reachable from the node 0, where a node is reachable if the distance is maxMoves or less.

Given the original graph and maxMoves, return the number of nodes that are reachable from node 0 in the new graph.

思路:如果将每个边的城市数cnt视为权重,实际上是求每个点到0点的权重的最短路径,之后再计算每条边有哪些城市可以到达。

1.最短路径,用Dijkstra 算法

2.需要注意顶点本身也有一个权重

class Solution {
public:
    int reachableNodes(vector<vector<int>>& edges, int maxMoves, int n) {
        vector<int>shortDis(n, -1);
        vector<vector<int> > mp(n, vector<int>(0)), lr(edges.size(), vector<int>(2,0));
        priority_queue<pair<int,int> > pq;
        for(int i = 0; i< edges.size(); i++) {
            mp[edges[i][0]].push_back(i);
            mp[edges[i][1]].push_back(i);
        }
        int rst = 0;
        pq.push({0,0});
        while(!pq.empty()) {
            auto top = pq.top();
            pq.pop();
            if (shortDis[top.second] != -1) continue;
            shortDis[top.second] = - top.first;
            if (shortDis[top.second] <= maxMoves) rst ++;
            for(auto i : mp[top.second]) {
                auto p = edges[i][0] != top.second ? edges[i][0] : edges[i][1];
                if (shortDis[p] != -1) continue;
                pq.push({top.first-edges[i][2]-1, p});
            }
        }
         for(int i = 0; i< edges.size(); i++) {
            auto x = edges[i][0], y = edges[i][1], d = 0;
            if (shortDis[x] == -1 || shortDis[y] == -1) continue;
            if (shortDis[x] > shortDis[y]) {swap(x,y); d = 1;}
            if (shortDis[x] > maxMoves) continue;
            getCnt(edges, lr, shortDis, i, rst,x,d, maxMoves);
            getCnt(edges, lr, shortDis, i, rst,y,d^1, maxMoves);
         }
        return rst;
    }
    inline void getCnt(vector<vector<int>> &edges, vector<vector<int>> &lr, vector<int> &shortDis, int &i, int &rst, int& x, int d, int maxMoves){
        auto tmp = min(maxMoves - shortDis[x], edges[i][2]);
        if (tmp <= lr[i][d]) return;
        if (tmp >= edges[i][2] - lr[i][d^1]) {
            rst += edges[i][2]  - lr[i][0] - lr[i][1];
            lr[i][d] = edges[i][2] - lr[i][d^1];
        } else {
            rst += tmp - lr[i][d];
            lr[i][d] = tmp;
        }
    }
};

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值