单源最短路径问题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,与源点直接相连的初始化为其权重,其他 ...
随机推荐
- mysql binlog相关
1.清除的binlog 删除所有binlog日志,新日志编号从头开始 RESET MASTER; 删除mysql-bin.XXXX之前所有日志 PURGE MASTER LOGS TO 'my ...
- Codeforces 1140F 线段树 分治 并查集
题意及思路:https://blog.csdn.net/u013534123/article/details/89010251 之前cf有一个和这个相似的题,不过那个题只有合并操作,没有删除操作,直接 ...
- Ubuntu下串口工具
一.Kermit 1.安装: sudo apt-get install ckermit 2.配置: sudo gedit /etc/kermit/kermrc 3.在文件末端添加如下内容 : set ...
- set,get方法(属性,索引器)
很多时候我们不可以把一些字段暴露出来允许别人调用和修改,为了隐藏这些字段又便于加限制的使用,在面向对象编程中一般采用写get set函数的办法,比如: //字段_age, "_"表 ...
- HTML5 原生API input file 来实现多图上传,并大图预览
闲来无事,突然想用原生来实现图片的多图上传. 一.效果图大致如下: 1.上传时可以选择多图 2.上传之后缩略图下过图如下: 3.点击缩略图,大图展示当前所点击的图片,并可以左右滑动查看其它的缩略图对应 ...
- jsp网站访问次数统计的几种方法
我采用的是jsp网页,但是不管采用什么语言,原理是一样的. 第一种,单页面统计.就是说,只要点击这个页面就会统计一次. <body> <%!//在这种标记中定义的变量为全局变量 in ...
- AutoCAD二次开发-使用ObjectARX向导创建应用程序(HelloWorld例子)
AutoCAD2007+vs2005 首先自己去网上搜索下载AutoCAD2007的ARX开发包. 解压后如下 打开后如下 classmap文件夹为C++类和.net类的框架图,是一个DWG文件. d ...
- Hdu-4757 Tree(可持久化字典树+lca)
题目链接:点这 我的github地址:点这 Problem Description Zero and One are good friends who always have fun wi ...
- selenium工作的大概原理
selenium的原理是什么? selenium的原理涉及到3个部分,分别是 浏览器 driver: 一般我们都会下载driver client: 也就是我们写的代码 client其实并不知道浏览器是 ...
- 屏幕左侧鼠标常驻,隐藏部分显示,文章鼠标常驻,隐藏部分隐藏(我的hexo next博客)
文章目录 如图 功能 代码 博客地址:https://mmmmmm.me 源码:https://github.com/dataiyangu?tab=repositories 如图 功能 最左侧添加透明 ...