Acwing 849. Dijkstra求最短路 I
题目描述
给定一个n个点m条边的有向图,图中可能存在重边和自环,所有边权均为正值。
请你求出1号点到n号点的最短距离,如果无法从1号点走到n号点,则输出-1。
输入
第一行包含整数n和m。
接下来m行每行包含三个整数x,y,z,表示点x和点y之间存在一条有向边,边长为z。
输出
输出一个整数,表示1号点到n号点的最短距离。
如果路径不存在,则输出-1。
输入输出样例
输入
3 3
1 2 2
2 3 1
1 3 4
输出
3
题目分析
Dijkstra
算法用于解决 单源且权值为正的最短路问题,时间复杂度为
O
(
N
2
)
O(N^2)
O(N2)。
对于单源最短路问题: 稠密图:朴素Dijkstra
算法 不稠密:堆优化的Dijkstra
算法。
基本思想是找到当前未确定的最短距离的点中距离最短的点,根据这一点计算其他路径的值。
int t=-1; //将t设置为-1 因为Dijkstra算法适用于不存在负权边的图
for(int j=1;j<=n;j++)
{
if(!st[j]&&(t==-1||dist[t]>dist[j]) //该步骤即寻找还未确定最短路的点中路径最短的点
t=j;
}
通过上述操作当前我们的t代表就是剩余未确定最短路的点中 路径最短的点
而与此同时该点的最短路径也已经确定我们将该点标记
st[t]=true;
然后用这个去更新其余未确定点的最短距离
for(int j=1;j<=n;j++)
dist[j]=min(dist[j],dist[t]+g[t][j]);
源码
package DataStructure;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
/**
* @Description
* @Author:PrinceHan
* @CreateTime:2022/4/7 08:37
*/
public class Dijkstra最短路径1 {
static final int N = 505;
static int[][] edge = new int[N][N];
static int[] dist = new int[N];
static boolean[] vis = new boolean[N];
static int n, m;
public static void main(String[] args) throws IOException {
for (int i = 0; i < N; i++) {
Arrays.fill(edge[i], 0xffffff);
}
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] s = reader.readLine().split(" ");
n = Integer.parseInt(s[0]);
m = Integer.parseInt(s[1]);
for (int i = 0; i < m; i++) {
int x, y, z;
String[] s1 = reader.readLine().split(" ");
x = Integer.parseInt(s1[0]);
y = Integer.parseInt(s1[1]);
z = Integer.parseInt(s1[2]);
edge[x][y] = Math.min(edge[x][y], z);//去掉重边
}
System.out.println(Dijkstra());
}
public static int Dijkstra() {
Arrays.fill(dist, 0xffffff);
dist[1] = 0;
for (int i = 0; i < n; i++) {
int t = -1;
for (int j = 1; j <= n; j++) {
if (!vis[j] && (t == -1 || dist[t] > dist[j]))
t = j;
}
vis[t] = true;
for (int j = 1; j <= n; j++) {
if (dist[j] > dist[t] + edge[t][j]) {
dist[j] = dist[t] + edge[t][j];
}
}
}
return dist[n] == 0xffffff ? -1 : dist[n];
}
}
Acwing 850. Dijkstra求最短路 II(堆优化)
题目描述
给定一个n个点m条边的有向图,图中可能存在重边和自环,所有边权均为正值。
请你求出1号点到n号点的最短距离,如果无法从1号点走到n号点,则输出-1。
输入
第一行包含整数n和m。
接下来m行每行包含三个整数x,y,z,表示点x和点y之间存在一条有向边,边长为z。
输出
输出一个整数,表示1号点到n号点的最短距离。
如果路径不存在,则输出-1。
输入输出样例
输入
3 3
1 2 2
2 3 1
1 3 4
输出
3
题目分析
基于稀疏图的堆优化的Dijkstra
算法,时间复杂度为
O
(
m
l
o
g
n
)
O(mlogn)
O(mlogn)。采用邻接表来存储图,用一个优先队列来维护最短的路径。
源码
package DataStructure;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;
/**
* @Description
* @Author:PrinceHan
* @CreateTime:2022/4/6 21:24
*/
public class Dijkstra最短路径堆优化 {
static final int N = 100010;
static int[] head = new int[N]; //h
static int[] next = new int[N]; //ne
static int[] edge = new int[N]; //w
static int[] elem = new int[N]; //e
static int[] dist = new int[N];
static int idx, n, m;
static boolean[] st = new boolean[N];
static class PII implements Comparable<PII> {
int distance, t;
PII(int distance, int t) {
this.distance = distance;
this.t = t;
}
@Override
public int compareTo(PII o) {
return distance - o.distance;
}
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] s = reader.readLine().split(" ");
n = Integer.parseInt(s[0]);
m = Integer.parseInt(s[1]);
Arrays.fill(head, -1);
for (int i = 0; i < m; i++) {
int x, y, z;
String[] s1 = reader.readLine().split(" ");
x = Integer.parseInt(s1[0]);
y = Integer.parseInt(s1[1]);
z = Integer.parseInt(s1[2]);
add(x, y, z);
}
System.out.println(Dijkstra());
}
public static int Dijkstra() {
Arrays.fill(dist, 0xffffff);
dist[1] = 0;
PriorityQueue<PII> PIIs = new PriorityQueue<>();
PIIs.add(new PII(0, 1));
while (!PIIs.isEmpty()) {
PII tmp = PIIs.poll();
int distance = tmp.distance;
int t = tmp.t;
if (st[t]) continue;
st[t] = true;
for (int i = head[t]; i != -1; i = next[i]) {
int j = elem[i];
if (dist[j] > edge[i] + distance) {
dist[j] = edge[i] + distance;
PIIs.add(new PII(dist[j], j));
}
}
}
return dist[n] == 0xffffff ? -1 : dist[n];
}
public static void add(int x, int y, int z) {
elem[idx] = y;
edge[idx] = z;
next[idx] = head[x];
head[x] = idx++;
}
}
路径
题目描述
输出
10266837
题目分析
朴素Dijkstra
算法
源码
package lanqiao.y2021_12;
import java.util.Arrays;
/**
* @Description
* @Author:PrinceHan
* @CreateTime:2022/4/6 20:33
*/
public class Dijkstra路径 {
static final int N = 2050, n = 2021;
static int[][] edge = new int[N][N];
static int[] dist = new int[N];
static boolean[] vis = new boolean[N];
public static void main(String[] args) {
for (int i = 1; i <= n; i++) {
Arrays.fill(edge[i], 0xffffff);
}
for (int i = 1; i <= 2021; i++) {
for (int j = i + 1; j <= i + 21 && j <= 2021; j++) {
int gcd = gcd(i, j);
edge[i][j] = i * j / gcd;
edge[j][i] = edge[i][j];
}
}
Dijkstra(1);
System.out.println(dist[2021]);
}
public static void Dijkstra(int x) {
Arrays.fill(dist, 0xffffff);
dist[x] = 0;
for (int i = 0; i < n; i++) {
int t = -1;
for (int j = 1; j <= n; j++) {
if (!vis[j] && (t == -1 || dist[t] > dist[j])) {
t = j;
}
}
vis[t] = true;
for (int j = 1; j <= n; j++) {
dist[j] = Math.min(dist[j], dist[t] + edge[t][j]);
}
}
}
static int gcd(int m, int n) {
return m % n == 0 ? n : gcd(n, m % n);
}
}