用邻接矩阵

/*
单源最短路径问题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算法)的更多相关文章

  1. 图论(四)------非负权有向图的单源最短路径问题,Dijkstra算法

    Dijkstra算法解决了有向图G=(V,E)上带权的单源最短路径问题,但要求所有边的权值非负. Dijkstra算法是贪婪算法的一个很好的例子.设置一顶点集合S,从源点s到集合中的顶点的最终最短路径 ...

  2. 单源最短路径问题之dijkstra算法

    欢迎探讨,如有错误敬请指正 如需转载,请注明出处 http://www.cnblogs.com/nullzx/ 1. 算法的原理 以源点开始,以源点相连的顶点作为向外延伸的顶点,在所有这些向外延伸的顶 ...

  3. 单源最短路径—Bellman-Ford和Dijkstra算法

    Bellman-Ford算法:通过对边进行松弛操作来渐近地降低从源结点s到每个结点v的最短路径的估计值v.d,直到该估计值与实际的最短路径权重相同时为止.该算法主要是基于下面的定理: 设G=(V,E) ...

  4. 单源最短路径问题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] */ ...

  5. 图->最短路径->单源最短路径(迪杰斯特拉算法Dijkstra)

    文字描述 引言:如下图一个交通系统,从A城到B城,有些旅客可能关心途中中转次数最少的路线,有些旅客更关心的是节省交通费用,而对于司机,里程和速度则是更感兴趣的信息.上面这些问题,都可以转化为求图中,两 ...

  6. 单源最短路径-迪杰斯特拉算法(Dijkstra's algorithm)

    Dijkstra's algorithm 迪杰斯特拉算法是目前已知的解决单源最短路径问题的最快算法. 单源(single source)最短路径,就是从一个源点出发,考察它到任意顶点所经过的边的权重之 ...

  7. 单源最短路径 Bellman_ford 和 dijkstra

    首先两个算法都是常用于 求单源最短路径 关键部分就在于松弛操作 实际上就是dp的感觉 if (dist[e.to] > dist[v] + e.cost) { dist[e.to] = dist ...

  8. PAT甲级——1111 Online Map (单源最短路经的Dijkstra算法、priority_queue的使用)

    本文章同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90041078   1111 Online Map (30 分) ...

  9. 单源最短路:Dijkstra算法 及 关于负权的讨论

    描述: 对于图(有向无向都适用),求某一点到其他任一点的最短路径(不能有负权边). 操作: 1. 初始化: 一个节点大小的数组dist[n] 源点的距离初始化为0,与源点直接相连的初始化为其权重,其他 ...

随机推荐

  1. windows系统exe文件图标变成了白色无图标

    转载:https://blog.csdn.net/whatday/article/details/52658412   在命令提示符下输入下列命令即可恢复.   按键 “WIN+R” 输入即可cmd ...

  2. redis-布隆过滤器使用

    占用空间测试地址 https://krisives.github.io/bloom-calculator/

  3. java输入一个整数N,打印1~n位数

    举个栗子:输入 3 : 打印1,2,3......999 这里要注意一个坑,不可以直接算出最大的数,然后从1开始打印 .因为当n足够大时,n位数必定会超出int范围和long范围 所以我们需要用字符串 ...

  4. Python内部变量与外部变量

    def outer(): x = 'outer x' def inner(): x = 'inner x' print(x) inner() print(x) # 这里的`x`与`x = 'outer ...

  5. c# 转16进制

    1.byte[] 转换16进制字符串 1.1 BitConverter方式 var str = DateTime.Now.ToString(); var encode = Encoding.UTF8; ...

  6. 「题解」:$Six$

    问题 A: Six 时间限制: 1 Sec  内存限制: 512 MB 题面 题面谢绝公开. 题解 来写一篇正经的题解. 每一个数对于答案的贡献与数本身无关,只与它包含了哪几个质因数有关. 所以考虑二 ...

  7. CSS template

    ylbtech-CSS3: 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://ylbtech.cn ...

  8. HDU 1700 Points on Cycle (坐标旋转)

    题目链接:HDU 1700 Problem Description There is a cycle with its center on the origin. Now give you a poi ...

  9. mysql中explain详解

    explain语法 有两种用法: 1.EXPLAIN tbl_name    2.EXPLAIN [EXTENDED] SELECT select_options 为了更好的说明它,我们需要建两张表, ...

  10. next()nextLine()以及nextInt()的区别及用法【转载】

    next().nextLine().nextInt()作为scanner内置的方法,常常让人傻傻分不清楚,今天在这里记下他们的区别以及以此区别为出发点的用法:他们的区别在于对于空格的处理方式不同,以及 ...