当前位置: 首页 > wzjs >正文

勒索做钓鱼网站的人广州网站建设那家好

勒索做钓鱼网站的人,广州网站建设那家好,黄冈seo顾问,做网站购买服务器吗图论part11 Floyd 算法精讲 代码随想录链接 题目链接 代码 三维DP数组 import java.util.Scanner;public class Main {// 定义最大距离值,避免使用Integer.MAX_VALUE防止加法溢出public static final int INF 100000000; // 10^8足够大且不会溢出public static…

图论part11

Floyd 算法精讲

代码随想录链接
题目链接

在这里插入图片描述

代码

三维DP数组
import java.util.Scanner;public class Main {// 定义最大距离值,避免使用Integer.MAX_VALUE防止加法溢出public static final int INF = 100000000; // 10^8足够大且不会溢出public static void main(String[] args) {Scanner sc = new Scanner(System.in);/* * 1. 输入处理* 第一行:N(景点数量), M(道路数量)* 接下来M行:u, v, w (双向道路)* 然后一行:Q(查询数量)* 接下来Q行:start, end (查询起点和终点)*/int n = sc.nextInt(); // 景点数量 (1到n编号)int m = sc.nextInt(); // 道路数量/** 2. 初始化三维DP数组* dp[k][i][j]表示从i到j,允许经过前k个节点(1..k)的最短路径* 这里k的范围是0到n:* - k=0表示不允许经过任何中间节点(直接边)* - k=n表示允许经过所有节点*/int[][][] dp = new int[n+1][n+1][n+1];// 初始化所有距离为INF,自己到自己的距离为0for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {for (int k = 0; k <= n; k++) {if (i == j) {dp[k][i][j] = 0; // 自己到自己的距离为0} else {dp[k][i][j] = INF; // 初始化为最大值}}}}// 3. 读取道路信息并填充初始距离(k=0的情况)for (int i = 0; i < m; i++) {int u = sc.nextInt();int v = sc.nextInt();int w = sc.nextInt();// 双向道路,两个方向都要设置dp[0][u][v] = w;dp[0][v][u] = w;}/* * 4. Floyd-Warshall算法核心部分(三维DP版本)* 状态转移方程:* dp[k][i][j] = min(dp[k-1][i][j], dp[k-1][i][k] + dp[k-1][k][j])* 含义:* - 从i到j允许经过前k个节点的最短路径 =*   min(不经过k的最短路径, 经过k的最短路径)*/for (int k = 1; k <= n; k++) {         // 中间节点for (int i = 1; i <= n; i++) {     // 起点for (int j = 1; j <= n; j++) { // 终点// 比较两种情况:// 1. 不经过节点k的最短路径(dp[k-1][i][j])// 2. 经过节点k的最短路径(dp[k-1][i][k] + dp[k-1][k][j])dp[k][i][j] = Math.min(dp[k-1][i][j], dp[k-1][i][k] + dp[k-1][k][j]);}}}// 5. 处理查询int q = sc.nextInt(); // 查询数量for (int i = 0; i < q; i++) {int start = sc.nextInt();int end = sc.nextInt();// 使用允许经过所有节点(n个)的最短路径作为结果// 如果不可达则输出-1System.out.println(dp[n][start][end] == INF ? -1 : dp[n][start][end]);}}
}
二维DP数组
import java.util.Scanner;public class Main {// 定义最大距离值,避免使用Integer.MAX_VALUE防止加法溢出public static final int INF = 100000000; // 10^8足够大且不会溢出public static void main(String[] args) {Scanner sc = new Scanner(System.in);// 1. 读取景点数量和道路数量int n = sc.nextInt(); // 景点数量 (1到n编号)int m = sc.nextInt(); // 道路数量// 2. 初始化距离矩阵int[][] dist = new int[n+1][n+1]; // 1-based索引// 初始化所有距离为INF,自己到自己的距离为0for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {if (i == j) {dist[i][j] = 0;} else {dist[i][j] = INF;}}}// 3. 读取道路信息并填充初始距离for (int i = 0; i < m; i++) {int u = sc.nextInt();int v = sc.nextInt();int w = sc.nextInt();// 双向道路,两个方向都要设置dist[u][v] = w;dist[v][u] = w;}// 4. Floyd-Warshall算法核心部分for (int k = 1; k <= n; k++) {         // 中间节点for (int i = 1; i <= n; i++) {     // 起点for (int j = 1; j <= n; j++) { // 终点// 如果通过中间节点k可以缩短i到j的距离if (dist[i][k] + dist[k][j] < dist[i][j]) {dist[i][j] = dist[i][k] + dist[k][j];}}}}// 5. 处理查询int q = sc.nextInt(); // 查询数量for (int i = 0; i < q; i++) {int start = sc.nextInt();int end = sc.nextInt();// 输出结果,如果不可达则输出-1System.out.println(dist[start][end] == INF ? -1 : dist[start][end]);}}
}

A * 算法精讲 (A star算法)

代码随想录链接
题目链接

在这里插入图片描述

代码(超时,示例正确)

import java.util.*;public class Main {// 定义骑士的8种可能移动方式(马走日)private static final int[][] MOVES = {{1, 2}, {2, 1}, {2, -1}, {1, -2},{-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}};// 节点类,表示棋盘上的一个位置static class Node implements Comparable<Node> {int x, y;        // 当前位置坐标int g;           // 从起点到当前节点的实际代价int h;           // 到目标节点的启发式估计代价Node parent;      // 父节点(用于路径回溯)public Node(int x, int y) {this.x = x;this.y = y;this.g = 0;this.h = 0;}// 计算总代价f = g + hpublic int f() {return g + h;}// 用于优先队列排序@Overridepublic int compareTo(Node other) {return Integer.compare(this.f(), other.f());}// 重写equals和hashCode用于比较节点@Overridepublic boolean equals(Object obj) {if (this == obj) return true;if (obj == null || getClass() != obj.getClass()) return false;Node node = (Node) obj;return x == node.x && y == node.y;}@Overridepublic int hashCode() {return Objects.hash(x, y);}}// A*算法实现public static int aStarKnightPath(int startX, int startY, int targetX, int targetY) {// 如果起点就是终点,直接返回0if (startX == targetX && startY == targetY) {return 0;}// 边界检查if (!isValid(startX, startY) || !isValid(targetX, targetY)) {return -1;}// 初始化开放列表和关闭列表PriorityQueue<Node> openList = new PriorityQueue<>();Set<Node> closedList = new HashSet<>();// 创建起点节点Node startNode = new Node(startX, startY);startNode.g = 0;startNode.h = heuristic(startX, startY, targetX, targetY);openList.add(startNode);while (!openList.isEmpty()) {// 获取当前最优节点Node currentNode = openList.poll();// 如果到达目标节点,返回步数if (currentNode.x == targetX && currentNode.y == targetY) {return currentNode.g;}// 将当前节点加入关闭列表closedList.add(currentNode);// 遍历所有可能的移动for (int[] move : MOVES) {int newX = currentNode.x + move[0];int newY = currentNode.y + move[1];// 检查新位置是否有效if (!isValid(newX, newY)) {continue;}// 创建新节点Node neighbor = new Node(newX, newY);neighbor.g = currentNode.g + 1;  // 每步代价为1neighbor.h = heuristic(newX, newY, targetX, targetY);neighbor.parent = currentNode;// 如果已经在关闭列表中,跳过if (closedList.contains(neighbor)) {continue;}// 检查是否在开放列表中boolean inOpenList = false;for (Node node : openList) {if (node.equals(neighbor)) {inOpenList = true;// 如果找到更优路径,更新节点if (neighbor.g < node.g) {node.g = neighbor.g;node.parent = currentNode;}break;}}// 如果不在开放列表中,加入if (!inOpenList) {openList.add(neighbor);}}}// 如果开放列表为空且未找到路径,返回-1return -1;}// 启发式函数:曼哈顿距离除以3(骑士每步最多移动3格)private static int heuristic(int x1, int y1, int x2, int y2) {return (Math.abs(x1 - x2) + Math.abs(y1 - y2)) / 3;}// 检查坐标是否有效private static boolean isValid(int x, int y) {return x >= 1 && x <= 1000 && y >= 1 && y <= 1000;}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 a2 = sc.nextInt();int b1 = sc.nextInt();int b2 = sc.nextInt();int steps = aStarKnightPath(a1, a2, b1, b2);System.out.println(steps);}}
}

最短路算法总结篇

代码随想录链接


图论总结篇

代码随想录链接


http://www.dtcms.com/wzjs/808942.html

相关文章:

  • 培训网站源码账号权重查询入口
  • 做视频网站的备案要求现在学seo课程多少钱
  • 电商网站设计流程图无锡企业网站制作
  • 羽贝网站建设建立网站平台
  • 领券的网站怎么建设官方网站开发制作
  • 如何做专题网站我做网站可以赚钱吗
  • 竟标网站源码桂林网站制作哪家好
  • 建设外贸网站公司丹阳网站建设公司
  • 个人网站建设完整教程安徽建设工程信息网官网优秀中项网
  • 济南住房与城乡建设局网站网站建设营销
  • 做影视网站的软件色盲和色弱的区别
  • 在那些免费网站做宣传效果好小制作小发明大全简单
  • 烟台外贸网站建设公司网站找不到首页
  • 淄川网站建设yx718wordpress英文版切换中文版
  • 企业网站如何去做优化网络工程就业前景好吗
  • 平面设计线下培训班多少钱网络推广seo培训班
  • 巴州网站建设宁波知名seo关键词优化
  • 网站建设短信福州模板做网站
  • 营销网络世界地图网站优化排名公司
  • 下列关于网站开发中网站上传wordpress菜单栏插件
  • 网站可以做多语言的吗货源网站程序
  • 网站关键词和网页关键词的样本设计工作室 网站
  • 网页设计网站有哪些免费店铺logo在线制作
  • 青岛 机械 中企动力提供网站建设天汇大厦网站建设公司
  • 编程 朋友 做网站网站建设的方法
  • h5网站还有哪些深圳抖音seo
  • 教师做网站赚钱聚名网页版
  • 手机网站 php做企业网站需要用到的软件
  • 企业网站建设策划书方案范文谷歌网络推广
  • 怎样做网站后台成都游戏软件开发公司有哪些