单源最短路径问题2 (Dijkstra算法)
用邻接矩阵
/*
单源最短路径问题2 (Dijkstra算法)
样例:
5 7
0 1 3
0 3 7
1 2 4
1 3 2
2 3 5
2 4 6
3 4 4
输出:
[0, 3, 7, 5, 9]
*/ import java.util.Arrays;
import java.util.Scanner; public class Main {
//图的顶点数,总边数
static int V, E;
//存储所有的边,大小为顶点数
static int[][] Edges;
static int[] d;
static boolean[] visited;
static final int MAX_VALUE = 999999; public static void main(String[] args) {
creatGraph();
shortPath(1);
System.out.println(Arrays.toString(d));
} static void shortPath(int start) {
d = new int[V];
visited = new boolean[V];
Arrays.fill(d, MAX_VALUE);
d[start] = 0;
while (true) {
int min_index = -1;
for (int j = 0; j < V; j++) {
if (!visited[j] && (min_index == -1 || d[j] < d[min_index])) {
min_index = j;
}
}
if (min_index == -1) break;
visited[min_index] = true;
for (int u = 0; u < V; u++) {
d[u] = Math.min(d[u], d[min_index] + Edges[min_index][u]);
}
}
} static void creatGraph() {
Scanner sc = new Scanner(System.in);
V = sc.nextInt();
E = sc.nextInt();
Edges = new int[V][V];
for (int[] i : Edges)
Arrays.fill(i, MAX_VALUE);
for (int i = 0; i < E; i++) {
int u = sc.nextInt();
int v = sc.nextInt();
int w = sc.nextInt();
Edges[u][v] = w;
Edges[v][u] = w;
}
}
}
用邻接表
/*
单源最短路径问题2 (Dijkstra算法)
样例:
5 7
0 1 3
0 3 7
1 2 4
1 3 2
2 3 5
2 4 6
3 4 4
输出:
[0, 3, 7, 5, 9]
*/ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner; public class Main {
//图的顶点数,总边数
static int V, E;
//存储所有的边,大小为顶点数
static ArrayList<Edge>[] Edges;
static int[] d;
static boolean[] visited;
static final int MAX_VALUE = 999999; public static void main(String[] args) {
creatGraph();
shortPath(1);
System.out.println(Arrays.toString(d));
} static void shortPath(int start) {
d = new int[V];
visited = new boolean[V];
Arrays.fill(d, MAX_VALUE);
d[start] = 0;
while (true) {
int min_index = -1;
for (int j = 0; j < V; j++) {
if (!visited[j] && (min_index == -1 || d[j] < d[min_index])) {
min_index = j;
}
}
if (min_index == -1) break;
visited[min_index] = true;
for (Edge i : Edges[min_index]) {
d[i.to] = Math.min(d[i.to], d[min_index] + i.cost);
}
}
} static void creatGraph() {
Scanner sc = new Scanner(System.in);
V = sc.nextInt();
E = sc.nextInt();
Edges = new ArrayList[V];
for (int i = 0; i < V; i++)
Edges[i] = new ArrayList();
for (int i = 0; i < E; i++) {
int u = sc.nextInt();
int v = sc.nextInt();
int w = sc.nextInt();
Edges[u].add(new Edge(v, w));
Edges[v].add(new Edge(u, w));
}
}
} class Edge {
int to;
int cost; public Edge(int to, int cost) {
this.to = to;
this.cost = cost;
}
}
单源最短路径问题2 (Dijkstra算法)的更多相关文章
- 图论(四)------非负权有向图的单源最短路径问题,Dijkstra算法
Dijkstra算法解决了有向图G=(V,E)上带权的单源最短路径问题,但要求所有边的权值非负. Dijkstra算法是贪婪算法的一个很好的例子.设置一顶点集合S,从源点s到集合中的顶点的最终最短路径 ...
- 单源最短路径问题之dijkstra算法
欢迎探讨,如有错误敬请指正 如需转载,请注明出处 http://www.cnblogs.com/nullzx/ 1. 算法的原理 以源点开始,以源点相连的顶点作为向外延伸的顶点,在所有这些向外延伸的顶 ...
- 单源最短路径—Bellman-Ford和Dijkstra算法
Bellman-Ford算法:通过对边进行松弛操作来渐近地降低从源结点s到每个结点v的最短路径的估计值v.d,直到该估计值与实际的最短路径权重相同时为止.该算法主要是基于下面的定理: 设G=(V,E) ...
- 单源最短路径问题1 (Bellman-Ford算法)
/*单源最短路径问题1 (Bellman-Ford算法)样例: 5 7 0 1 3 0 3 7 1 2 4 1 3 2 2 3 5 2 4 6 3 4 4 输出: [0, 3, 7, 5, 9] */ ...
- 图->最短路径->单源最短路径(迪杰斯特拉算法Dijkstra)
文字描述 引言:如下图一个交通系统,从A城到B城,有些旅客可能关心途中中转次数最少的路线,有些旅客更关心的是节省交通费用,而对于司机,里程和速度则是更感兴趣的信息.上面这些问题,都可以转化为求图中,两 ...
- 单源最短路径-迪杰斯特拉算法(Dijkstra's algorithm)
Dijkstra's algorithm 迪杰斯特拉算法是目前已知的解决单源最短路径问题的最快算法. 单源(single source)最短路径,就是从一个源点出发,考察它到任意顶点所经过的边的权重之 ...
- 单源最短路径 Bellman_ford 和 dijkstra
首先两个算法都是常用于 求单源最短路径 关键部分就在于松弛操作 实际上就是dp的感觉 if (dist[e.to] > dist[v] + e.cost) { dist[e.to] = dist ...
- PAT甲级——1111 Online Map (单源最短路经的Dijkstra算法、priority_queue的使用)
本文章同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90041078 1111 Online Map (30 分) ...
- 单源最短路:Dijkstra算法 及 关于负权的讨论
描述: 对于图(有向无向都适用),求某一点到其他任一点的最短路径(不能有负权边). 操作: 1. 初始化: 一个节点大小的数组dist[n] 源点的距离初始化为0,与源点直接相连的初始化为其权重,其他 ...
随机推荐
- 三重循环之break
while(1) { while(1) { while(1) { break; } } } //逻辑堪比绝技啊 脑洞开之注册表循环遍历方法
- webpack output.publicPath
output.publicPath string function 对于按需加载(on-demand-load)或加载外部资源(external resources)(如图片.文件等)来说,outpu ...
- 【LeetCode】Stack
[503] Next Greater Element II [Medium] 给一个循环数组,找到离当前元素最近的比它大的元素. Input: [1,2,1] Output: [2,-1,2] Exp ...
- 控制台Cannot read property 'disabled' of null报错的问题
点击任何东西控制台都会报错: 也没有提示哪儿出了问题,后来我就代码一块一块的注释,终于找到了原因. 我在项目中用了 el-dropdown ,但是没有用他的el-dropdown-menu 所以才会一 ...
- kubeadm部署多master节点高可用k8s1.16.2
一.架构信息 系统版本:CentOS 7.6 内核:3.10.0‐1062.4.1.el7.x86_64 Kubernetes: v1.16.2 Dockerce: 19.03 推荐硬件配置:2核4 ...
- leetcode-162周赛-1254-统计封闭岛屿数量
题目描述: 自己的提交: class Solution: def closedIsland(self, grid: List[List[int]]) -> int: def dfs(grid,r ...
- Kylin-2.6.2集群部署
1. 集群节点规划与说明 rzx1 all rzx2 query rzx3 query 说明: Kylin节点角色有三种: all: 包含query和job query: 查询节点 job: 工作节点 ...
- How To Use PostgreSQL with Your Ruby on Rails Application on Ubuntu 14.04
How To Use PostgreSQL with Your Ruby on Rails Application on Ubuntu 14.04 链接来自于:https://www.digitalo ...
- 管理mysql
要管理MySQL,可以使用可视化图形界面MySQL Workbench. MySQL Workbench可以用可视化的方式查询.创建和修改数据库表,但是,归根到底,MySQL Workbench是一个 ...
- TeleportPoint可瞬移的目标位置
TeleportPoint.png 最外部的组件: Animation: 包含了该装置的几个动画Teleport Point: 点传送的脚本 teleport_marker_mesh: 外部的圆柱形光 ...