图论part11
Floyd 算法精讲
代码随想录链接
题目链接

代码
三维DP数组
import java.util.Scanner
;
public
class Main {
public
static
final
int INF = 100000000
;
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++
) {
if (i == j) {
dp[k][i][j] = 0
;
}
else {
dp[k][i][j] = INF
;
}
}
}
}
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;
}
for (
int k = 1
; k <= n; k++
) {
for (
int i = 1
; i <= n; i++
) {
for (
int j = 1
; j <= n; j++
) {
dp[k][i][j] = Math.min(dp[k-1][i][j]
, dp[k-1][i][k] + dp[k-1][k][j]
)
;
}
}
}
int q = sc.nextInt(
)
;
for (
int i = 0
; i < q; i++
) {
int start = sc.nextInt(
)
;
int end = sc.nextInt(
)
;
System.out.println(dp[n][start][end] == INF ? -1 : dp[n][start][end]
)
;
}
}
}
二维DP数组
import java.util.Scanner
;
public
class Main {
public
static
final
int INF = 100000000
;
public
static
void main(String[] args) {
Scanner sc =
new Scanner(System.in)
;
int n = sc.nextInt(
)
;
int m = sc.nextInt(
)
;
int[][] dist =
new
int[n+1][n+1]
;
for (
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
;
}
}
}
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;
}
for (
int k = 1
; k <= n; k++
) {
for (
int i = 1
; i <= n; i++
) {
for (
int j = 1
; j <= n; j++
) {
if (dist[i][k] + dist[k][j] < dist[i][j]
) {
dist[i][j] = dist[i][k] + dist[k][j]
;
}
}
}
}
int q = sc.nextInt(
)
;
for (
int i = 0
; i < q; i++
) {
int start = sc.nextInt(
)
;
int end = sc.nextInt(
)
;
System.out.println(dist[start][end] == INF ? -1 : dist[start][end]
)
;
}
}
}
A * 算法精讲 (A star算法)
代码随想录链接
题目链接

代码(超时,示例正确)
import java.util.*
;
public
class Main {
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
;
}
public
int f(
) {
return g + h;
}
@Override
public
int compareTo(Node other) {
return Integer.compare(
this.f(
)
, other.f(
)
)
;
}
@Override
public
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;
}
@Override
public
int hashCode(
) {
return Objects.hash(x, y)
;
}
}
public
static
int aStarKnightPath(
int startX,
int startY,
int targetX,
int targetY) {
if (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
;
neighbor.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)
;
}
}
}
return -1
;
}
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)
;
}
}
}
最短路算法总结篇
代码随想录链接
图论总结篇
代码随想录链接