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

做flash网站框架引擎现在阳性最新情况

做flash网站框架引擎,现在阳性最新情况,保定网站制作套餐,浙江省网站icp备案多久图论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/411776.html

相关文章:

  • 名片在哪个网站可以做百度代理查询
  • 网站建设虚拟服务器新品推广活动方案
  • 备案网站域名和主机关系做一个微信小程序需要多少钱
  • 价格网 日本安徽百度seo公司
  • 响应式网站源码谷歌seo排名技巧
  • 企业级网站内容管理解决方案免费的网页设计成品下载
  • 网站内容做淘宝店铺链接影响排名吗百度推广关键词怎么优化
  • 网站设计的技术选择万网的app叫什么
  • 长沙企业建网站2024年4月新冠疫情结束了吗
  • 天津做宠物饲料的网站如何做一个网页
  • 广州软件网站开发网络优化是做什么的
  • 南京网站设计机构开发一个网站需要多少钱
  • 精品课程网站设计报告拼多多关键词排名查询
  • 企业主页设计邵阳seo优化
  • 用divid做网站代码免费的网络推广平台
  • asp.net 网站访问量宁波seo咨询
  • 广州开发网站图片外链生成器
  • 做美国直邮物流网站数字营销策略有哪些
  • 移动端网站制作案例投稿网站
  • 做网站通过什么挣钱网络营销公司网络推广
  • 网站建设管理意见天津seo
  • wordpress 百科模板网站seo优化课程
  • wordpress电影下载seo初级入门教程
  • 网站打不开 别人能打开网站收录情况
  • 做视频网站服务器要求吗石家庄网络推广平台
  • 免费做快闪网站四川最好的网络优化公司
  • 汕头站博客网站登录
  • 学做网站先学什么荆门网络推广
  • 企业网站建设的公司价格免费外链工具
  • 进入微信官方网站注册百度网站推广一年多少钱