图论-最短路径 floyd/dijkstra-Find the City With the Smallest Number of Neighbors at a Threshold Distance
2020-01-30 22:22:58
问题描述:
问题求解:
解法一:floyd
这个题目一看就是floyd解最合适,因为是要求多源最短路,floyd算法是最合适的,时间复杂度为O(n ^ 3)。
- int inf = (int)1e9;
- public int findTheCity(int n, int[][] edges, int distanceThreshold) {
- int[][] dp = new int[n][n];
- for (int i = 0; i < n; i++) Arrays.fill(dp[i], inf);
- for (int i = 0; i < n; i++) {
- dp[i][i] = 0;
- }
- for (int[] edge : edges) {
- int u = edge[0];
- int v = edge[1];
- int d = edge[2];
- dp[u][v] = d;
- dp[v][u] = d;
- }
- for (int k = 0; k < n; k++) {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- if (dp[i][j] > dp[i][k] + dp[k][j]) {
- dp[i][j] = dp[i][k] + dp[k][j];
- }
- }
- }
- }
- List<int[]> note = new ArrayList<>();
- for (int i = 0; i < n; i++) {
- int cnt = 0;
- for (int j = 0; j < n; j++) {
- if (dp[i][j] <= distanceThreshold) cnt += 1;
- }
- note.add(new int[]{i, cnt});
- }
- Collections.sort(note, new Comparator<int[]>(){
- public int compare(int[] o1, int[] o2) {
- return o1[1] == o2[1] ? o2[0] - o1[0] : o1[1] - o2[1];
- }
- });
- return note.get(0)[0];
- }
解法二:dijkstra
使用邻接表 + 优先队列可以将单源最短路的时间复杂度降到O(ElogV),所以整体的时间复杂度为O(VElogV)。
- public int findTheCity(int n, int[][] edges, int distanceThreshold) {
- List<int[]> record = new ArrayList<>();
- List<int[]>[] graph = new List[n];
- for (int i = 0; i < n; i++) graph[i] = new ArrayList<>();
- for (int[] edge : edges) {
- int from = edge[0];
- int to = edge[1];
- int w = edge[2];
- graph[from].add(new int[]{to, w});
- graph[to].add(new int[]{from, w});
- }
- for (int i = 0; i < n; i++) {
- int[] dist = new int[n];
- Arrays.fill(dist, (int)1e9);
- helper(graph, i, dist);
- int cnt = 0;
- for (int j = 0; j < n; j++) if (dist[j] <= distanceThreshold) cnt += 1;
- record.add(new int[]{i, cnt});
- }
- Collections.sort(record, (int[] o1, int[] o2) -> o1[1] == o2[1] ? o2[0] - o1[0] : o1[1] - o2[1]);
- return record.get(0)[0];
- }
- private void helper(List<int[]>[] graph, int node, int[] dist) {
- int n = graph.length;
- PriorityQueue<int[]> pq = new PriorityQueue<>((int[] o1, int[] o2) -> o1[1] - o2[1]);
- int[] used = new int[n];
- pq.add(new int[]{node, 0});
- while (!pq.isEmpty()) {
- int[] curr = pq.poll();
- int from = curr[0];
- int d = curr[1];
- if (used[from] == 1) continue;
- used[from] = 1;
- dist[from] = d;
- for (int[] next : graph[from]) {
- int to = next[0];
- int w = next[1];
- if (dist[to] > dist[from] + w) {
- dist[to] = dist[from] + w;
- pq.add(new int[]{to, dist[to]});
- }
- }
- }
- }
图论-最短路径 floyd/dijkstra-Find the City With the Smallest Number of Neighbors at a Threshold Distance的更多相关文章
- 图论-最短路径 2.Dijkstra算法O (N2)
2.Dijkstra算法O (N2) 用来计算从一个点到其他所有点的最短路径的算法,是一种单源最短路径算法.也就是说,只能计算起点只有一个的情况. Dijkstra的时间复杂度是O (N2),它不能处 ...
- 图论最短路径算法——Dijkstra
说实在的,这算法很简单,很简单,很简单--因为它是贪心的,而且码量也小,常数比起SPFA也小. 主要思想 先初始化,dis[起点]=0,其它皆为无限大. 还要有一个bz数组,bz[i]表示i是否确定为 ...
- 单源最短路径Dijkstra算法,多源最短路径Floyd算法
1.单源最短路径 (1)无权图的单源最短路径 /*无权单源最短路径*/ void UnWeighted(LGraph Graph, Vertex S) { std::queue<Vertex&g ...
- 经典树与图论(最小生成树、哈夫曼树、最短路径问题---Dijkstra算法)
参考网址: https://www.jianshu.com/p/cb5af6b5096d 算法导论--最小生成树 最小生成树:在连通网的所有生成树中,所有边的代价和最小的生成树,称为最小生成树. im ...
- 最短路径算法——Dijkstra,Bellman-Ford,Floyd-Warshall,Johnson
根据DSqiu的blog整理出来 :http://dsqiu.iteye.com/blog/1689163 PS:模板是自己写的,如有错误欢迎指出~ 本文内容框架: §1 Dijkstra算法 §2 ...
- 最短路径算法-Dijkstra算法的应用之单词转换(词梯问题)(转)
一,问题描述 在英文单词表中,有一些单词非常相似,它们可以通过只变换一个字符而得到另一个单词.比如:hive-->five:wine-->line:line-->nine:nine- ...
- 最短路径之Dijkstra算法和Floyd-Warshall算法
最短路径算法 最短路径算法通常用在寻找图中任意两个结点之间的最短路径或者是求全局最短路径,像是包括Dijkstra.A*.Bellman-Ford.SPFA(Bellman-Ford的改进版本).Fl ...
- 最短路径问题---Dijkstra算法详解
侵删https://blog.csdn.net/qq_35644234/article/details/60870719 前言 Nobody can go back and start a new b ...
- 最短路径问题-Dijkstra
概述 与前面说的Floyd算法相比,Dijkstra算法只能求得图中特定顶点到其余所有顶点的最短路径长度,即单源最短路径问题. 算法思路 1.初始化,集合K中加入顶点v,顶点v到其自身的最短距离为0, ...
随机推荐
- 网购分期不还 N种恶果等着你
N种恶果等着你" title="网购分期不还 N种恶果等着你"> 网购市场狂飙突进的发展,让每个人都享受到随时随地购物的乐趣,也在很大程度上推动商品之间的流通.目前 ...
- Webpack 常用 modules
@(Javascript)[webpack] babel babel-core: babel 核心程式,知道如何載入程式碼.解析和輸出檔案(但不包含編譯). babel-loader: 用來告訴 ba ...
- Oracle最大进程连接数问题
问题描述 分析报告保存功能,在本地测试使用时可以正常保存:但是部署在客户现场的系统该功能无法保存成功(全部保存): ---->代码功能没有问题,问题应该在服务器配置或者数据库配置等方面出现问题: ...
- 【C++基础】008常量和变量
简介:常量和变量. 常量和变量 1. 常量 具体把数据写出来 2,3,4: 1.2,1.3: "Hello World!","C++": cout <&l ...
- Redis(2)——跳跃表
一.跳跃表简介 跳跃表(skiplist)是一种随机化的数据结构,由 William Pugh 在论文<Skip lists: a probabilistic alternative to ba ...
- C++走向远洋——48(项目一1、复数类中的运算符重载、类的成员函数)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- python递归用法
需求:4的阶乘 4*3*2*1计算.通过递归算法,c=4*getnums(4-1),然后调用自己本身的函数,形成递归,就等于3*getnums(3-1),2*getnums(2-1),依次递归调用,最 ...
- 原生js中的常用方法的写法
1.js深度克隆的方法 //第一种 function deepClone(obj){ var str,newObj = obj instanceof Array? [] : {}; if(typeof ...
- Hibernage错误:Could not open Hibernate Session for transaction
今天客户发来的错误,是SSH框架做的项目,是用户在登陆时候出现的错误,但刷新之后就没问题. 提示错误:Could not open Hibernate Session for transaction. ...
- JAVA 16bit CRC_CCITT
JAVA 16bit CRC_CCITT public class CRC_CCITT { static int CRC16_ccitt_table[] = { 0x0000, 0x1189, 0x2 ...