关于时间复杂度,用n来表示节点的个数,m表示边数
单源最短路径
Dijkstra
朴素Dijkstra
时间复杂度: O ( n 2 ) O({n^2}) O(n2)
package 图;
import java.util.*;
/**
* 最短路径算法
*/
public class Dijkstra1 {
public static void main(String[] args) {
Dijkstra(1);
}
/**
* 朴素版本
* 有向图的最短路路径
* 从s点出发,到达其他点的最短路径
* @param s
*/
public static void Dijkstra(int s){
int N = 510,INF = 0x3f3f3f3f;
int[][] g = new int[N][N];
boolean[] st = new boolean[N];
Scanner in = new Scanner(System.in);
int n = in.nextInt(); // 顶点数
int m = in.nextInt(); // 边数
int[] dist = new int[n + 1];
for(int i = 1;i <= n;i ++) Arrays.fill(g[i],INF);
while(m --> 0){
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
g[a][b] = Math.min(g[a][b],c);// 这个因为可能存在重边,所以选择出最短的
g[b][a] = g[a][b];
}
Arrays.fill(dist,INF);
dist[s] = 0;
for(int i = 0;i < n;i ++){
int t = -1;
// 找到未加入结果集的最短的距离
for(int j = 1;j <= n;j ++){
if(!st[j] && (t == -1 || dist[j] < dist[t])){
t = j;
}
}
st[t] = true;
// 进行松弛操作
for(int j = 1;j <= n;j ++){
if(!st[j])dist[j] = Math.min(dist[j],dist[t] + g[t][j]);
}
}
System.out.println(Arrays.toString(dist));
}
}
堆优化的Dijskta
时间复杂度: O ( m l o g n ) O({mlogn}) O(mlogn)
package 图;
import java.util.*;
/**
* dijkstra的堆优化版本
* 稀疏图的时候或者边数很大的时候
*/
public class Dijkstra2 {
static final int N = (int) (1.5 * 1e5), INF = 0x3f3f3f3f;
static int[] h = new int[N], e = new int[N], ne = new int[N], w = new int[N], dist = new int[N];
static boolean[] st = new boolean[N];
static int n, m, idx;
public static void main(String[] args) {
Arrays.fill(dist, INF);
Arrays.fill(h, -1);
idx = 0;
Scanner in = new Scanner(System.in);
n = in.nextInt();
m = in.nextInt();
while(m --> 0){
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
add(a, b, c);
}
dijkstra(1);
}
public static void add(int a, int b, int c){
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx ++;
}
/**
* 基于优先队列的dijkstra
* @param u
*/
public static void dijkstra(int u){
Queue<pos> q = new PriorityQueue<>((a,b) -> a.dist - b.dist);
dist[u] = 0;
q.offer(new pos(u, dist[u]));
while(!q.isEmpty()){
pos p = q.poll();
if(st[p.index]) continue;
st[p.index] = true;
for(int i = h[p.index]; i != -1; i = ne[i]){
int j = e[i];
if(p.dist + w[i] < dist[j]){
dist[j] = p.dist + w[i];
q.offer(new pos(j, dist[j]));
}
}
}
for (int i = 1; i <= n; i++) {
System.out.print(dist[i] + " ");
}
}
}
class pos{
int index;
int dist;
public pos(int index, int dist) {
this.index = index;
this.dist = dist;
}
}
Bellman_Ford
时间复杂度: O ( n m ) O({nm}) O(nm)
package 图;
import java.util.*;
public class Bellman {
static final int N = 510, M = 100010, INF = 0x3f3f3f3f;
static edge[] g = new edge[M];
static int n, m;
static int[] dist = new int[N];
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n = in.nextInt();
m = in.nextInt();
for(int i = 0; i < m; i ++){
int a, b, w;
a = in.nextInt();
b = in.nextInt();
w = in.nextInt();
g[i] = new edge(a, b, w);
}
bellman_ford(1);
System.out.println();
System.out.println(hasminusCycle());
}
/**
* 从u到其他个点的最短距离
* @param u
*/
public static void bellman_ford(int u){
Arrays.fill(dist,INF);
dist[u] = 0;
for(int i = 0; i < n; i ++){
for(int j = 0; j < m; j ++){
edge e = g[j];
dist[e.b] = Math.min(dist[e.b], dist[e.a] + e.w);
}
}
for(int i = 1; i <= n; i ++){
System.out.print(dist[i] + " ");
}
}
/**
* 判断是否存在负环
* 第n次迭代发现dist数组还可以改变那么就一定存在负环
* @return
*/
public static String hasminusCycle(){
for(int j = 0; j < m; j ++){
edge e = g[j];
if(dist[e.a] + e.w < dist[e.b]){
return "YES";
}
}
return "NO";
}
}
class edge{
int a, b, w;
public edge() {
}
public edge(int a, int b, int w) {
this.a = a;
this.b = b;
this.w = w;
}
}
SPFA
时间复杂度: O ( m ) O({m}) O(m)
package 图;
import java.util.*;
public class SPFA {
static final int N = 100010, INF = 0x3f3f3f3f;
static int[] h = new int[N], e = new int[N], ne = new int[N], w = new int[N], dist = new int[N];
static boolean[] st = new boolean[N];
static int n, m, idx;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Arrays.fill(dist, INF);
Arrays.fill(h, -1);
idx = 0;
n = in.nextInt();
m = in.nextInt();
for(int i = 0; i < m; i ++){
int a, b, c;
a = in.nextInt();
b = in.nextInt();
c = in.nextInt();
add(a, b, c);
}
spfa(1);
}
public static void add(int a, int b, int c){
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx ++;
}
public static void spfa(int u){
Queue<Integer> q = new ArrayDeque<>();
q.offer(u);
dist[u] = 0;
st[u] = true;
while(!q.isEmpty()){
int p = q.poll();
st[p] = false;
for(int i = h[p]; i != -1; i = ne[i]){
int j = e[i];
if(dist[j] > dist[p] + w[i]){
dist[j] = dist[p] + w[i];
if(!st[j]){
q.offer(j);
st[j] = true;
}
}
}
}
for(int i = 1; i <= n; i ++){
System.out.print(dist[i] + " ");
}
}
}
多源汇最短路径
Floyd算法
时间复杂度: O ( n 3 ) O({n^3}) O(n3)
package 图;
import java.util.*;
public class Floyd{
static final int N = 210, M = 20010, INF = 0x3f3f3f3f;
static int n, m, q;
static int[][] g = new int[N][N];
static boolean[] st = new boolean[N];
public static void main(String[] args){
for(int i = 0; i < N; i ++){
Arrays.fill(g[i], INF);
g[i][i] = 0;
}
Scanner in = new Scanner(System.in);
n = in.nextInt();
m = in.nextInt();
q = in.nextInt();
for(int i = 0; i < m; i ++){
int a, b, c;
a = in.nextInt();
b = in.nextInt();
c = in.nextInt();
g[a][b] = Math.min(g[a][b], c);
}
Floyd();
while(q --> 0){
int u, v;
u = in.nextInt();
v = in.nextInt();
// 有可能5->n的权值是负数且dist[5] = INF,那么dist[n] < INF,到5时不可达的,到n也一定是不可达的
// 而且根据题目数据范围,可以确定,都是负权边的时候,dist[n]极限是INF - 500*10000也是大于INF/2的
if(g[u][v] > INF / 2)System.out.println("impossible");
else System.out.println(g[u][v]);
}
}
public static void Floyd(){
for(int k = 1; k <= n; k ++){
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= n; j ++){
g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
}
}
}
}
}