day 60 图论part11

文章目录

  • [Floyd 算法精讲 97. 小明逛公园](#Floyd 算法精讲 97. 小明逛公园)
  • [A * 算法精讲 (A star算法)127. 骑士的攻击](#A * 算法精讲 (A star算法)127. 骑士的攻击)
  • 最短路算法总结篇
  • 图论总结篇

Floyd 算法精讲 97. 小明逛公园

注意三维dp数组的含义,从i到j需要能够经过k个节点的最短路径。

java 复制代码
import java.util.*;

class Main {
    public static int MAX_VAL = 10005;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][][] dp = new int[n + 1][n + 1][n + 1];
        for (int i = 1; i <=n; i++) {
            for (int j = 1; j <=n; j++) {
                for(int k = 0; k <= n; k++) {
                    dp[i][j][k] = MAX_VAL;
                    
                }
            }
        }
        for (int i = 0; i < m; i++) {
            int u = sc.nextInt();
            int v = sc.nextInt();
            int w = sc.nextInt();
            dp[u][v][0] = w;
            dp[v][u][0] = w;
        }
        int q = sc.nextInt();
        for (int k = 1; k <= n; k++) {
            for (int i = 1; i <=n; i++) {
                for (int j = 1;j <= n; j++) {
                    dp[i][j][k] = Math.min(dp[i][j][k - 1], dp[i][k][k - 1] + dp[k][j][k - 1]);
                }
            }
        }
        for (int i = 0; i < q; i++) {
            int src = sc.nextInt();
            int dst = sc.nextInt();
            if (dp[src][dst][n] == MAX_VAL) {
                System.out.println(-1);
            }
            else {
                System.out.println(dp[src][dst][n]);
            }
        }

    }
}

A * 算法精讲 (A star算法)127. 骑士的攻击

定义一个启发式函数来优化BFS算法。

java 复制代码
import java.util.*;


public class Main {
    private static final int[][] MOVES = {
        {1, 2}, {2, 1}, {-1, 2}, {2, -1},
        {1, -2}, {-2, 1}, {-1, -2}, {-2, -1}
    };
    static class Node implements Comparable<Node> {
        int x, y, steps;
        double priority;

        Node(int x, int y, int steps, double priority) {
            this.x = x;
            this.y = y;
            this.steps = steps;
            this.priority = priority;
        }

        @Override
        public int compareTo(Node other) {
            return Double.compare(this.priority, other.priority);
        }
    }
    private static double getDistance(int x1, int y1, int x2, int y2) {
        return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
    }

    public static int aStar(int startX, int startY, int endX, int endY) {
        if (startX == endX && startY == endY) return 0;
        PriorityQueue<Node> pq = new PriorityQueue<>();

        Map<String, Integer> minSteps = new HashMap<>();

        String startKey = startX + "," + startY;
        pq.add(new Node(startX, startY, 0, getDistance(startX, startY, endX, endY)));
        minSteps.put(startKey, 0);


        while (!pq.isEmpty()) {
            Node curr = pq.poll();
            if (curr.x == endX && curr.y == endY) {
                return curr.steps;
            }

            for (int[] move : MOVES) {
                int nextX = curr.x + move[0];
                int nextY = curr.y + move[1];
                if (nextX >= 1 && nextX <= 1000 && nextY >= 1 && nextY <= 1000) {
                    int nextSteps = curr.steps + 1;
                    double nextpriority = nextSteps + getDistance(nextX, nextY, endX, endY);
                    pq.add(new Node(nextX, nextY, nextSteps, nextpriority));
                }
             }
        }
        return -1;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            int a1 = sc.nextInt();
            int b1 = sc.nextInt();
            int a2 = sc.nextInt();
            int b2 = sc.nextInt();
            System.out.println(aStar(a1, b1, a2, b2));
        }
    }
 }

最短路算法总结篇

dijkstra朴素版

dijkstra堆优化版

Bellman_ford

Bellman_ford 队列优化算法(又名SPFA)

bellman_ford 算法判断负权回路

bellman_ford之单源有限最短路

Floyd 算法精讲

启发式搜索:A * 算法

图论总结篇

深搜与广搜

并查集

最小生成树

拓扑排序

最短路算法

相关推荐
TDengine (老段)11 分钟前
TDengine 免费版说明
java·大数据·数据库·物联网·时序数据库·tdengine
码上有光15 分钟前
异常和智能指针
java·大数据·c++·servlet·异常·智能指针
变量未定义~18 分钟前
最小生成树1(Prim模板)、最小生成树2(kruskal模板)、最近公共祖先(模板)
算法
这就是佬们吗21 分钟前
Python入门⑤-异常处理、文件操作与实战项目
开发语言·数据库·python·算法·pycharm
LONGZETECH29 分钟前
新能源汽车充电设备装配与调试仿真教学软件 技术架构与核心实现解析
大数据·算法·3d·unity·架构·汽车
自学小白菜31 分钟前
关于提升机器学习算法做预测回归任务精度的方法分享
算法·机器学习·回归·提升精度
学废了wuwu39 分钟前
从入门到实战:深入浅出模型压缩三大核心——剪枝、彩票假设与知识蒸馏
人工智能·算法·剪枝
小肝一下1 小时前
3. 单链表
c语言·数据结构·c++·算法·leetcode·链表·dijkstra
劈星斩月1 小时前
监督学习算法 之 决策树
学习·算法·决策树
学计算机的计算基1 小时前
操作系统内存管理全解:虚拟内存、页表、COW、malloc、OOM一篇搞定
java·笔记·算法