LeetCode 1334. 阈值距离内邻居最少的城市:多次运用单源最短路的迪杰斯特拉算法

【LetMeFly】1334.阈值距离内邻居最少的城市:多次运用单源最短路的迪杰斯特拉算法

力扣题目链接:https://leetcode.cn/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/

n 个城市,按从 0n-1 编号。给你一个边数组 edges,其中 edges[i] = [fromi, toi, weighti] 代表 fromitoi两个城市之间的双向加权边,距离阈值是一个整数 distanceThreshold

返回能通过某些路径到达其他城市数目最少、且路径距离 最大distanceThreshold 的城市。如果有多个这样的城市,则返回编号最大的城市。

注意,连接城市 ij 的路径的距离等于沿该路径的所有边的权重之和。

示例 1:

复制代码
输入:n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4
输出:3
解释:城市分布图如上。
每个城市阈值距离 distanceThreshold = 4 内的邻居城市分别是:
城市 0 -> [城市 1, 城市 2] 
城市 1 -> [城市 0, 城市 2, 城市 3] 
城市 2 -> [城市 0, 城市 1, 城市 3] 
城市 3 -> [城市 1, 城市 2] 
城市 0 和 3 在阈值距离 4 以内都有 2 个邻居城市,但是我们必须返回城市 3,因为它的编号最大。

示例 2:

复制代码
输入:n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2
输出:0
解释:城市分布图如上。 
每个城市阈值距离 distanceThreshold = 2 内的邻居城市分别是:
城市 0 -> [城市 1] 
城市 1 -> [城市 0, 城市 4] 
城市 2 -> [城市 3, 城市 4] 
城市 3 -> [城市 2, 城市 4]
城市 4 -> [城市 1, 城市 2, 城市 3] 
城市 0 在阈值距离 2 以内只有 1 个邻居城市。

提示:

  • 2 <= n <= 100
  • 1 <= edges.length <= n * (n - 1) / 2
  • edges[i].length == 3
  • 0 <= fromi < toi < n
  • 1 <= weighti, distanceThreshold <= 10^4
  • 所有 (fromi, toi) 都是不同的。

方法一:多次运用单源最短路的迪杰斯特拉算法

迪杰斯特拉算法可以让我们在 O ( n 2 ) O(n^2) O(n2)的时间复杂度内求出图中某点到其他所有点的最短路径。

关于单源最短路的迪杰斯特拉算法,推荐查看某人视频讲解及配套代码。(算法本质是在所有能走的路中选一个最短的能到新节点的路来走)

这样,我们可以写一个函数来获取某个点不超过"distanceThreshold"的"邻居城市"的个数。

使用两个变量分别记录"最少的近邻个数"和"当前答案",遍历一遍每个节点,计算并更新这两个变量即可得到答案。

  • 时间复杂度 O ( n 3 ) O(n^3) O(n3)
  • 空间复杂度 O ( s i z e ( g r a p h ) + n ) O(size(graph) + n) O(size(graph)+n)

AC代码

C++
cpp 复制代码
class Solution {
private:
    int find1City(vector<vector<pair<int, int>>> &graph, int start, int Md) {
        vector<bool> visited(graph.size(), false);
        vector<int> minDistance(graph.size(), 1e9);
        minDistance[start] = 0;
        for (int i = 0; i < graph.size(); i++) {
            int thisMinDistance = 1e9;
            int thisShortestPoint = -1;
            for (int j = 0; j < graph.size(); j++) {
                if (!visited[j] && minDistance[j] < thisMinDistance) {
                    thisMinDistance = minDistance[j];
                    thisShortestPoint = j;
                }
            }
            if (thisMinDistance == 1e9) {
                break;
            }
            visited[thisShortestPoint] = true;
            for (auto& [toPoint, thisDistance] : graph[thisShortestPoint]) {
                if (minDistance[thisShortestPoint] + thisDistance < minDistance[toPoint]) {
                    minDistance[toPoint] = minDistance[thisShortestPoint] + thisDistance;
                }
            }
        }
        int ans = -1;
        for (int i = 0; i < graph.size(); i++) {
            if (minDistance[i] <= Md) {
                ans++;
            }
        }
        return ans;
    }
public:
    int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {
        vector<vector<pair<int, int>>> graph(n);
        for (auto& v : edges) {
            graph[v[0]].push_back({v[1], v[2]});
            graph[v[1]].push_back({v[0], v[2]});
        }
        int mCan = n, Mnum = 0;
        for (int i = 0; i < n; i++) {
            int thisCity = find1City(graph, i, distanceThreshold);
            if (thisCity <= mCan) {
                mCan = thisCity;
                Mnum = i;
            }
        }
        return Mnum;
    }
};

同步发文于CSDN,原创不易,转载经作者同意后请附上原文链接哦~

Tisfy:https://letmefly.blog.csdn.net/article/details/134410277

相关推荐
evans在进步5 小时前
LeetCode 64:最小路径和——Java 原地动态规划详解
java·leetcode·动态规划
Tisfy5 小时前
LeetCode 1386.安排电影院座位:哈希表+位运算
算法·leetcode·散列表·题解·哈希表
Nil2086 小时前
leetcode 199二叉树的右视图
算法·leetcode·深度优先
土司大王10 小时前
LeetCode hot100——两两交换链表中的节点
算法·leetcode·职场和发展
zander25812 小时前
LeetCode 300. 最长递增子序列
算法·leetcode·深度优先
欧叶冲冲冲12 小时前
Python常见数据结构的CRUD(LeetCode高频版速查)
数据结构·python·leetcode
圣保罗的大教堂13 小时前
leetcode 3622. 判断整除性 简单
leetcode
Lyyaoo.13 小时前
【普通数组】【中等】除了自身以外数组的乘积
数据结构·算法·leetcode
圣保罗的大教堂15 小时前
leetcode 2996. 大于等于顺序前缀和的最小缺失整数 简单
leetcode
圣保罗的大教堂15 小时前
leetcode 3702. 按位异或非零的最长子序列 中等
leetcode