class065 A星、Floyd、Bellman-Ford與SPFA【算法】
2023-12-9 19:27:02
算法講解065【必備】A星、Floyd、Bellman-Ford與SPFA
code1 A*算法模版
// A*算法模版(對數器驗證)
package class065;import java.util.PriorityQueue;// A*算法模版(對數器驗證)
public class Code01_AStarAlgorithm {// 0:上,1:右,2:下,3:左public static int[] move = new int[] { -1, 0, 1, 0, -1 };// Dijkstra算法// grid[i][j] == 0 代表障礙// grid[i][j] == 1 代表道路// 只能走上、下、左、右,不包括斜線方向// 返回從(startX, startY)到(targetX, targetY)的最短距離public static int minDistance1(int[][] grid, int startX, int startY, int targetX, int targetY) {if (grid[startX][startY] == 0 || grid[targetX][targetY] == 0) {return -1;}int n = grid.length;int m = grid[0].length;int[][] distance = new int[n][m];for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {distance[i][j] = Integer.MAX_VALUE;}}distance[startX][startY] = 1;boolean[][] visited = new boolean[n][m];PriorityQueue<int[]> heap = new PriorityQueue<>((a, b) -> a[2] - b[2]);// 0 : 行// 1 : 列// 2 : 從源點出發到達當前點的距離heap.add(new int[] { startX, startY, 1 });while (!heap.isEmpty()) {int[] cur = heap.poll();int x = cur[0];int y = cur[1];if (visited[x][y]) {continue;}visited[x][y] = true;if (x == targetX && y == targetY) {return distance[x][y];}for (int i = 0, nx, ny; i < 4; i++) {nx = x + move[i];ny = y + move[i + 1];if (nx >= 0 && nx < n && ny >= 0 && ny < m && grid[nx][ny] == 1 && !visited[nx][ny]&& distance[x][y] + 1 < distance[nx][ny]) {distance[nx][ny] = distance[x][y] + 1;heap.add(new int[] { nx, ny, distance[x][y] + 1 });}}}return -1;}// A*算法// grid[i][j] == 0 代表障礙// grid[i][j] == 1 代表道路// 只能走上、下、左、右,不包括斜線方向// 返回從(startX, startY)到(targetX, targetY)的最短距離public static int minDistance2(int[][] grid, int startX, int startY, int targetX, int targetY) {if (grid[startX][startY] == 0 || grid[targetX][targetY] == 0) {return -1;}int n = grid.length;int m = grid[0].length;int[][] distance = new int[n][m];for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {distance[i][j] = Integer.MAX_VALUE;}}distance[startX][startY] = 1;boolean[][] visited = new boolean[n][m];// 0 : 行// 1 : 列// 2 : 從源點出發到達當前點的距離 + 當前點到終點的預估距離PriorityQueue<int[]> heap = new PriorityQueue<>((a, b) -> a[2] - b[2]);heap.add(new int[] { startX, startY, 1 + f1(startX, startY, targetX, targetY) });while (!heap.isEmpty()) {int[] cur = heap.poll();int x = cur[0];int y = cur[1];if (visited[x][y]) {continue;}visited[x][y] = true;if (x == targetX && y == targetY) {return distance[x][y];}for (int i = 0, nx, ny; i < 4; i++) {nx = x + move[i];ny = y + move[i + 1];if (nx >= 0 && nx < n && ny >= 0 && ny < m && grid[nx][ny] == 1 && !visited[nx][ny]&& distance[x][y] + 1 < distance[nx][ny]) {distance[nx][ny] = distance[x][y] + 1;heap.add(new int[] { nx, ny, distance[x][y] + 1 + f1(nx, ny, targetX, targetY) });}}}return -1;}// 曼哈頓距離public static int f1(int x, int y, int targetX, int targetY) {return (Math.abs(targetX - x) + Math.abs(targetY - y));}// 對角線距離public static int f2(int x, int y, int targetX, int targetY) {return Math.max(Math.abs(targetX - x), Math.abs(targetY - y));}// 歐式距離public static double f3(int x, int y, int targetX, int targetY) {return Math.sqrt(Math.pow(targetX - x, 2) + Math.pow(targetY - y, 2));}// 為了測試public static int[][] randomGrid(int n) {int[][] grid = new int[n][n];for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {if (Math.random() < 0.3) {// 每個格子有30%概率是0grid[i][j] = 0;} else {// 每個格子有70%概率是1grid[i][j] = 1;}}}return grid;}// 為了測試public static void main(String[] args) {int len = 100;int testTime = 10000;System.out.println("功能測試開始");for (int i = 0; i < testTime; i++) {int n = (int) (Math.random() * len) + 2;int[][] grid = randomGrid(n);int startX = (int) (Math.random() * n);int startY = (int) (Math.random() * n);int targetX = (int) (Math.random() * n);int targetY = (int) (Math.random() * n);int ans1 = minDistance1(grid, startX, startY, targetX, targetY);int ans2 = minDistance2(grid, startX, startY, targetX, targetY);if (ans1 != ans2) {System.out.println("出錯了!");}}System.out.println("功能測試結束");System.out.println("性能測試開始");int[][] grid = randomGrid(4000);int startX = 0;int startY = 0;int targetX = 3900;int targetY = 3900;long start, end;start = System.currentTimeMillis();int ans1 = minDistance1(grid, startX, startY, targetX, targetY);end = System.currentTimeMillis();System.out.println("運行dijskra算法結果: " + ans1 + ", 運行時間(毫秒) : " + (end - start));start = System.currentTimeMillis();int ans2 = minDistance2(grid, startX, startY, targetX, targetY);end = System.currentTimeMillis();System.out.println("運行A*算法結果: " + ans2 + ", 運行時間(毫秒) : " + (end - start));System.out.println("性能測試結束");}}
code2 P2910 [USACO08OPEN] Clear And Present Danger S
// Floyd算法模版(洛谷)
// 測試鏈接 : https://www.luogu.com.cn/problem/P2910
// 請同學們務必參考如下代碼中關于輸入、輸出的處理
// 這是輸入輸出處理效率很高的寫法
// 提交以下所有代碼,把主類名改成Main,可以直接通過
package class065;// Floyd算法模版(洛谷)
// 測試鏈接 : https://www.luogu.com.cn/problem/P2910
// 請同學們務必參考如下代碼中關于輸入、輸出的處理
// 這是輸入輸出處理效率很高的寫法
// 提交以下所有代碼,把主類名改成Main,可以直接通過import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;public class Code02_Floyd {public static int MAXN = 101;public static int MAXM = 10001;public static int[] path = new int[MAXM];public static int[][] distance = new int[MAXN][MAXN];public static int n, m, ans;// 初始時設置任意兩點之間的最短距離為無窮大,表示任何路不存在public static void build() {for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {distance[i][j] = Integer.MAX_VALUE;}}}public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));StreamTokenizer in = new StreamTokenizer(br);PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));while (in.nextToken() != StreamTokenizer.TT_EOF) {n = (int) in.nval;in.nextToken();m = (int) in.nval;for (int i = 0; i < m; i++) {in.nextToken();path[i] = (int) in.nval - 1;}// 這道題給的圖是鄰接矩陣的形式// 任意兩點之間的邊權都會給定// 所以顯得distance初始化不太必要// 但是一般情況下,distance初始化一定要做build();for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {in.nextToken();distance[i][j] = (int) in.nval;}}floyd();ans = 0;for (int i = 1; i < m; i++) {ans += distance[path[i - 1]][path[i]];}out.println(ans);}out.flush();out.close();br.close();}public static void floyd() {// O(N^3)的過程// 枚舉每個跳板// 注意,跳板要最先枚舉!跳板要最先枚舉!跳板要最先枚舉!for (int bridge = 0; bridge < n; bridge++) { // 跳板for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {// i -> .....bridge .... -> j// distance[i][j]能不能縮短// distance[i][j] = min ( distance[i][j] , distance[i][bridge] + distance[bridge][j])if (distance[i][bridge] != Integer.MAX_VALUE && distance[bridge][j] != Integer.MAX_VALUE&& distance[i][j] > distance[i][bridge] + distance[bridge][j]) {distance[i][j] = distance[i][bridge] + distance[bridge][j];}}}}}}
code3 787. K 站中轉內最便宜的航班
// Bellman-Ford算法應用(不是模版)
// k站中轉內最便宜的航班
// 有 n 個城市通過一些航班連接。給你一個數組 flights
// 其中 flights[i] = [fromi, toi, pricei]
// 表示該航班都從城市 fromi 開始,以價格 pricei 抵達 toi。
// 現在給定所有的城市和航班,以及出發城市 src 和目的地 dst,你的任務是找到出一條最多經過 k 站中轉的路線
// 使得從 src 到 dst 的 價格最便宜 ,并返回該價格。 如果不存在這樣的路線,則輸出 -1。
// 測試鏈接 : https://leetcode.cn/problems/cheapest-flights-within-k-stops/
package class065;import java.util.Arrays;// Bellman-Ford算法應用(不是模版)
// k站中轉內最便宜的航班
// 有 n 個城市通過一些航班連接。給你一個數組 flights
// 其中 flights[i] = [fromi, toi, pricei]
// 表示該航班都從城市 fromi 開始,以價格 pricei 抵達 toi。
// 現在給定所有的城市和航班,以及出發城市 src 和目的地 dst,你的任務是找到出一條最多經過 k 站中轉的路線
// 使得從 src 到 dst 的 價格最便宜 ,并返回該價格。 如果不存在這樣的路線,則輸出 -1。
// 測試鏈接 : https://leetcode.cn/problems/cheapest-flights-within-k-stops/
public class Code03_BellmanFord {// Bellman-Ford算法// 針對此題改寫了松弛邏輯,課上講了細節public static int findCheapestPrice(int n, int[][] flights, int start, int target, int k) {int[] cur = new int[n];Arrays.fill(cur, Integer.MAX_VALUE);cur[start] = 0;for (int i = 0; i <= k; i++) {int[] next = Arrays.copyOf(cur, n);for (int[] edge : flights) {// a -> b , wif (cur[edge[0]] != Integer.MAX_VALUE) {next[edge[1]] = Math.min(next[edge[1]], cur[edge[0]] + edge[2]);}}cur = next;}return cur[target] == Integer.MAX_VALUE ? -1 : cur[target];}}
P3385 【模板】負環
// Bellman-Ford + SPFA優化模版(洛谷)
// 給定一個 n個點的有向圖,請求出圖中是否存在從頂點 1 出發能到達的負環
// 負環的定義是:一條邊權之和為負數的回路。
// 測試鏈接 : https://www.luogu.com.cn/problem/P3385
// 請同學們務必參考如下代碼中關于輸入、輸出的處理
// 這是輸入輸出處理效率很高的寫法
// 提交以下所有代碼,把主類名改成Main,可以直接通過
package class065;// Bellman-Ford + SPFA優化模版(洛谷)
// 給定一個 n個點的有向圖,請求出圖中是否存在從頂點 1 出發能到達的負環
// 負環的定義是:一條邊權之和為負數的回路。
// 測試鏈接 : https://www.luogu.com.cn/problem/P3385
// 請同學們務必參考如下代碼中關于輸入、輸出的處理
// 這是輸入輸出處理效率很高的寫法
// 提交以下所有代碼,把主類名改成Main,可以直接通過import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;public class Code04_SPFA {public static int MAXN = 2001;public static int MAXM = 6001;// 鏈式前向星建圖需要public static int[] head = new int[MAXN];public static int[] next = new int[MAXM];public static int[] to = new int[MAXM];public static int[] weight = new int[MAXM];public static int cnt;// SPFA需要public static int MAXQ = 4000001;// 源點出發到每個節點的距離表public static int[] distance = new int[MAXN];// 節點被松弛的次數public static int[] updateCnt = new int[MAXN];// 哪些節點被松弛了放入隊列public static int[] queue = new int[MAXQ];public static int l, r;// 節點是否已經在隊列中public static boolean[] enter = new boolean[MAXN];public static void build(int n) {cnt = 1;l = r = 0;Arrays.fill(head, 1, n + 1, 0);Arrays.fill(enter, 1, n + 1, false);Arrays.fill(distance, 1, n + 1, Integer.MAX_VALUE);Arrays.fill(updateCnt, 1, n + 1, 0);}public static void addEdge(int u, int v, int w) {next[cnt] = head[u];to[cnt] = v;weight[cnt] = w;head[u] = cnt++;}public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));StreamTokenizer in = new StreamTokenizer(br);PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));in.nextToken();int cases = (int) in.nval;for (int i = 0, n, m; i < cases; i++) {in.nextToken(); n = (int) in.nval;in.nextToken(); m = (int) in.nval;build(n);for (int j = 0, u, v, w; j < m; j++) {in.nextToken(); u = (int) in.nval;in.nextToken(); v = (int) in.nval;in.nextToken(); w = (int) in.nval;if (w >= 0) {addEdge(u, v, w);addEdge(v, u, w);} else {addEdge(u, v, w);}}out.println(spfa(n) ? "YES" : "NO");}out.flush();out.close();br.close();}// Bellman-Ford + SPFA優化的模版public static boolean spfa(int n) {distance[1] = 0;updateCnt[1]++;queue[r++] = 1;enter[1] = true;while (l < r) {int u = queue[l++];enter[u] = false;for (int ei = head[u], v, w; ei > 0; ei = next[ei]) {v = to[ei];w = weight[ei];if (distance[u] + w < distance[v]) {distance[v] = distance[u] + w;if (!enter[v]) {if (updateCnt[v]++ == n) {return true;}queue[r++] = v;enter[v] = true;}}}}return false;}}
2023-12-9 21:16:55