2020-01-30 22:22:58

问题描述

问题求解

解法一:floyd

这个题目一看就是floyd解最合适,因为是要求多源最短路,floyd算法是最合适的,时间复杂度为O(n ^ 3)。

  1. int inf = (int)1e9;
  2.  
  3. public int findTheCity(int n, int[][] edges, int distanceThreshold) {
  4. int[][] dp = new int[n][n];
  5. for (int i = 0; i < n; i++) Arrays.fill(dp[i], inf);
  6. for (int i = 0; i < n; i++) {
  7. dp[i][i] = 0;
  8. }
  9. for (int[] edge : edges) {
  10. int u = edge[0];
  11. int v = edge[1];
  12. int d = edge[2];
  13. dp[u][v] = d;
  14. dp[v][u] = d;
  15. }
  16. for (int k = 0; k < n; k++) {
  17. for (int i = 0; i < n; i++) {
  18. for (int j = 0; j < n; j++) {
  19. if (dp[i][j] > dp[i][k] + dp[k][j]) {
  20. dp[i][j] = dp[i][k] + dp[k][j];
  21. }
  22. }
  23. }
  24. }
  25. List<int[]> note = new ArrayList<>();
  26. for (int i = 0; i < n; i++) {
  27. int cnt = 0;
  28. for (int j = 0; j < n; j++) {
  29. if (dp[i][j] <= distanceThreshold) cnt += 1;
  30. }
  31. note.add(new int[]{i, cnt});
  32. }
  33. Collections.sort(note, new Comparator<int[]>(){
  34. public int compare(int[] o1, int[] o2) {
  35. return o1[1] == o2[1] ? o2[0] - o1[0] : o1[1] - o2[1];
  36. }
  37. });
  38. return note.get(0)[0];
  39. }

解法二:dijkstra

使用邻接表 + 优先队列可以将单源最短路的时间复杂度降到O(ElogV),所以整体的时间复杂度为O(VElogV)。

  1. public int findTheCity(int n, int[][] edges, int distanceThreshold) {
  2. List<int[]> record = new ArrayList<>();
  3. List<int[]>[] graph = new List[n];
  4. for (int i = 0; i < n; i++) graph[i] = new ArrayList<>();
  5. for (int[] edge : edges) {
  6. int from = edge[0];
  7. int to = edge[1];
  8. int w = edge[2];
  9. graph[from].add(new int[]{to, w});
  10. graph[to].add(new int[]{from, w});
  11. }
  12. for (int i = 0; i < n; i++) {
  13. int[] dist = new int[n];
  14. Arrays.fill(dist, (int)1e9);
  15. helper(graph, i, dist);
  16. int cnt = 0;
  17. for (int j = 0; j < n; j++) if (dist[j] <= distanceThreshold) cnt += 1;
  18. record.add(new int[]{i, cnt});
  19. }
  20. Collections.sort(record, (int[] o1, int[] o2) -> o1[1] == o2[1] ? o2[0] - o1[0] : o1[1] - o2[1]);
  21. return record.get(0)[0];
  22. }
  23.  
  24. private void helper(List<int[]>[] graph, int node, int[] dist) {
  25. int n = graph.length;
  26. PriorityQueue<int[]> pq = new PriorityQueue<>((int[] o1, int[] o2) -> o1[1] - o2[1]);
  27. int[] used = new int[n];
  28. pq.add(new int[]{node, 0});
  29. while (!pq.isEmpty()) {
  30. int[] curr = pq.poll();
  31. int from = curr[0];
  32. int d = curr[1];
  33. if (used[from] == 1) continue;
  34. used[from] = 1;
  35. dist[from] = d;
  36. for (int[] next : graph[from]) {
  37. int to = next[0];
  38. int w = next[1];
  39. if (dist[to] > dist[from] + w) {
  40. dist[to] = dist[from] + w;
  41. pq.add(new int[]{to, dist[to]});
  42. }
  43. }
  44. }
  45. }

  

  

图论-最短路径 floyd/dijkstra-Find the City With the Smallest Number of Neighbors at a Threshold Distance的更多相关文章

  1. 图论-最短路径 2.Dijkstra算法O (N2)

    2.Dijkstra算法O (N2) 用来计算从一个点到其他所有点的最短路径的算法,是一种单源最短路径算法.也就是说,只能计算起点只有一个的情况. Dijkstra的时间复杂度是O (N2),它不能处 ...

  2. 图论最短路径算法——Dijkstra

    说实在的,这算法很简单,很简单,很简单--因为它是贪心的,而且码量也小,常数比起SPFA也小. 主要思想 先初始化,dis[起点]=0,其它皆为无限大. 还要有一个bz数组,bz[i]表示i是否确定为 ...

  3. 单源最短路径Dijkstra算法,多源最短路径Floyd算法

    1.单源最短路径 (1)无权图的单源最短路径 /*无权单源最短路径*/ void UnWeighted(LGraph Graph, Vertex S) { std::queue<Vertex&g ...

  4. 经典树与图论(最小生成树、哈夫曼树、最短路径问题---Dijkstra算法)

    参考网址: https://www.jianshu.com/p/cb5af6b5096d 算法导论--最小生成树 最小生成树:在连通网的所有生成树中,所有边的代价和最小的生成树,称为最小生成树. im ...

  5. 最短路径算法——Dijkstra,Bellman-Ford,Floyd-Warshall,Johnson

    根据DSqiu的blog整理出来 :http://dsqiu.iteye.com/blog/1689163 PS:模板是自己写的,如有错误欢迎指出~ 本文内容框架: §1 Dijkstra算法 §2 ...

  6. 最短路径算法-Dijkstra算法的应用之单词转换(词梯问题)(转)

    一,问题描述 在英文单词表中,有一些单词非常相似,它们可以通过只变换一个字符而得到另一个单词.比如:hive-->five:wine-->line:line-->nine:nine- ...

  7. 最短路径之Dijkstra算法和Floyd-Warshall算法

    最短路径算法 最短路径算法通常用在寻找图中任意两个结点之间的最短路径或者是求全局最短路径,像是包括Dijkstra.A*.Bellman-Ford.SPFA(Bellman-Ford的改进版本).Fl ...

  8. 最短路径问题---Dijkstra算法详解

    侵删https://blog.csdn.net/qq_35644234/article/details/60870719 前言 Nobody can go back and start a new b ...

  9. 最短路径问题-Dijkstra

    概述 与前面说的Floyd算法相比,Dijkstra算法只能求得图中特定顶点到其余所有顶点的最短路径长度,即单源最短路径问题. 算法思路 1.初始化,集合K中加入顶点v,顶点v到其自身的最短距离为0, ...

随机推荐

  1. 网购分期不还 N种恶果等着你

    N种恶果等着你" title="网购分期不还 N种恶果等着你"> 网购市场狂飙突进的发展,让每个人都享受到随时随地购物的乐趣,也在很大程度上推动商品之间的流通.目前 ...

  2. Webpack 常用 modules

    @(Javascript)[webpack] babel babel-core: babel 核心程式,知道如何載入程式碼.解析和輸出檔案(但不包含編譯). babel-loader: 用來告訴 ba ...

  3. Oracle最大进程连接数问题

    问题描述 分析报告保存功能,在本地测试使用时可以正常保存:但是部署在客户现场的系统该功能无法保存成功(全部保存): ---->代码功能没有问题,问题应该在服务器配置或者数据库配置等方面出现问题: ...

  4. 【C++基础】008常量和变量

    简介:常量和变量. 常量和变量 1. 常量 具体把数据写出来 2,3,4: 1.2,1.3: "Hello World!","C++": cout <&l ...

  5. Redis(2)——跳跃表

    一.跳跃表简介 跳跃表(skiplist)是一种随机化的数据结构,由 William Pugh 在论文<Skip lists: a probabilistic alternative to ba ...

  6. C++走向远洋——48(项目一1、复数类中的运算符重载、类的成员函数)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  7. python递归用法

    需求:4的阶乘 4*3*2*1计算.通过递归算法,c=4*getnums(4-1),然后调用自己本身的函数,形成递归,就等于3*getnums(3-1),2*getnums(2-1),依次递归调用,最 ...

  8. 原生js中的常用方法的写法

    1.js深度克隆的方法 //第一种 function deepClone(obj){ var str,newObj = obj instanceof Array? [] : {}; if(typeof ...

  9. Hibernage错误:Could not open Hibernate Session for transaction

    今天客户发来的错误,是SSH框架做的项目,是用户在登陆时候出现的错误,但刷新之后就没问题. 提示错误:Could not open Hibernate Session for transaction. ...

  10. JAVA 16bit CRC_CCITT

    JAVA 16bit CRC_CCITT public class CRC_CCITT { static int CRC16_ccitt_table[] = { 0x0000, 0x1189, 0x2 ...