图论-最短路径 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, ...
随机推荐
- Redis: userd_memory使用超出maxmemory
Redis:userd_memory使用超出maxmemory 一.问题现象 2018.12.30 19:26分,收到Redis实例内存使用告警“内存使用率299%>=80%”,检查实例info ...
- CS229 Lesson 13 高斯混合模型
课程视频地址:http://open.163.com/special/opencourse/machinelearning.html 课程主页:http://cs229.stanford.edu/ 更 ...
- 阿里为何要用独立APP挖微信微商墙角?
微商,这个被很多人看来是逃离马云魔咒,和淘宝抗衡的电商模式,自诞生到狂飙就伴随着种种争议.由于传播效率极强,在很长时间里也一直是不少人口中津津乐道的神话故事和救市良方.以至于,淘宝推出各种手段封杀 ...
- STL容器的使用
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- @JsonFormat、@DateTimeFormat注解,读取数据库晚一天问题
@DateTimeFormat(pattern = "yyyy-MM-dd") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss&qu ...
- python-面向对象小结
面向对象 小结 1:面向对象:就是一种编程思想 简称oop,指挥某某完成能完成的功能 2:面向对象与面向过程的优缺点 : 面向过程: 优点: 复杂问题简答化(一步一步解决), 流程化, 缺点:机械化 ...
- 研究开源源码之Myrmec
好久没写博客了,自己也弄不清是懒了还是忙了.毕竟白天需要工作,晚上有时候看看资料,有时候陪家人,有时候约朋友......更加累了,可能由于累了就懒得总结了. 今天有同事问我关于代码检查文件类型的问题. ...
- C++ STL迭代器原理和简单实现
1. 迭代器简介 为了提高C++编程的效率,STL(Standard Template Library)中提供了许多容器,包括vector.list.map.set等.然而有些容器(vector)可以 ...
- 三星最先进EUV产线投用
近日,三星宣布,在韩国华城工业园新开一条专司 EUV(极紫外光刻)技术的晶圆代工产线 V1,最次量产 7nm. 据悉,V1 产线/工厂 2018 年 2 月动工,2019 年下半年开始测试晶圆生产,首 ...
- vue项目 github 上传项目并链接地址
git init git init: 通过命令git init把这个文件夹变成Git可管理的仓库git status git status:查看当前仓库状态 git add . 这里提示你虽然把项目粘 ...