当前位置:   article > 正文

leetcode 882. Reachable Nodes In Subdivided Graph

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.需要注意顶点本身也有一个权重

  1. class Solution {
  2. public:
  3. int reachableNodes(vector<vector<int>>& edges, int maxMoves, int n) {
  4. vector<int>shortDis(n, -1);
  5. vector<vector<int> > mp(n, vector<int>(0)), lr(edges.size(), vector<int>(2,0));
  6. priority_queue<pair<int,int> > pq;
  7. for(int i = 0; i< edges.size(); i++) {
  8. mp[edges[i][0]].push_back(i);
  9. mp[edges[i][1]].push_back(i);
  10. }
  11. int rst = 0;
  12. pq.push({0,0});
  13. while(!pq.empty()) {
  14. auto top = pq.top();
  15. pq.pop();
  16. if (shortDis[top.second] != -1) continue;
  17. shortDis[top.second] = - top.first;
  18. if (shortDis[top.second] <= maxMoves) rst ++;
  19. for(auto i : mp[top.second]) {
  20. auto p = edges[i][0] != top.second ? edges[i][0] : edges[i][1];
  21. if (shortDis[p] != -1) continue;
  22. pq.push({top.first-edges[i][2]-1, p});
  23. }
  24. }
  25. for(int i = 0; i< edges.size(); i++) {
  26. auto x = edges[i][0], y = edges[i][1], d = 0;
  27. if (shortDis[x] == -1 || shortDis[y] == -1) continue;
  28. if (shortDis[x] > shortDis[y]) {swap(x,y); d = 1;}
  29. if (shortDis[x] > maxMoves) continue;
  30. getCnt(edges, lr, shortDis, i, rst,x,d, maxMoves);
  31. getCnt(edges, lr, shortDis, i, rst,y,d^1, maxMoves);
  32. }
  33. return rst;
  34. }
  35. inline void getCnt(vector<vector<int>> &edges, vector<vector<int>> &lr, vector<int> &shortDis, int &i, int &rst, int& x, int d, int maxMoves){
  36. auto tmp = min(maxMoves - shortDis[x], edges[i][2]);
  37. if (tmp <= lr[i][d]) return;
  38. if (tmp >= edges[i][2] - lr[i][d^1]) {
  39. rst += edges[i][2] - lr[i][0] - lr[i][1];
  40. lr[i][d] = edges[i][2] - lr[i][d^1];
  41. } else {
  42. rst += tmp - lr[i][d];
  43. lr[i][d] = tmp;
  44. }
  45. }
  46. };

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/946434
推荐阅读
相关标签
  

闽ICP备14008679号