图论-最短路径 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, ...
随机推荐
- ES常见问题整理
1.集群状态red.yellow处理方法 1.red表示主分片数据不完整,通常时由于某个索引的主分片为分片unassigned,找出这个分片未分配的原因,解决即可: curl -XGET http:/ ...
- Ubuntu18.04制作本地源
Ubuntu 18.04 制作本地源 1. 在可联网的Ubuntu18.04上制作源 创建目录 mkdir /opt/debs 最好在目标电脑上创建相同的目录,以免 apt-get install 时 ...
- 表单验证之JQuery Validate控件
概述 jQuery Validation Plugin v1.14.0,基于JQuery,官网http://jqueryvalidation.org/ 该插件捆绑了一套有用的验证方法,包括 URL 和 ...
- ECMA5中定义的对象属性特性和方法
ECMA5规定了只有内部才有的特性,描述了属性的各种特征,这些特性用于实现JavaScript引擎,因此在Js中不能直接访问他们.为了标识特性,我们一般会他们放入两对方括号中. ECMAScript中 ...
- C++走向远洋——31(六周,项目一,1.1)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:fenshu.cpp * 作者:常轩 * 微信公众号:World ...
- sublime安装vue插件
1.打开sublime text 3按 Ctrl+Shift+P(相信你有单身的手速,同时按完这3个键) 2.选中上图中,框出来的内容,按下enter. 3.选择上图的第二个即:vue syntax ...
- python django 之 django自带的分页
1. 例1: 基础的分页 1). vim app01/views.py def users(request): from django.core.pagina ...
- ArrayList集合不能使用foreach增加、删除、修改元素的原因
大家先看两段代码 第一段代码: List<String> arrayList1 = new ArrayList<String>(); arrayList1.add(" ...
- 正式学习MVC 03
1.View -> Controller的数据通信 1) 通过url查询字符串 public ActionResult Index(string user) { return Content(u ...
- 前端ps中常用的操作
昨天,ui给了个psd图,让写成网页.额,要自己切图.很久之前,操作的还凑乎.但是,好久了,都忘了.所以,打算自己记个笔记,方便以后查看. 首先,打开ps就先来设置一下ps的单位啦点击最上面的一行的编 ...