专题四 最短路练习
2. Frogger原题链接
湖中有n块石头,编号从1到n,有两只青蛙,A在1号石头上,B在2号石头上,B想去看望A。但是A的石头超出了他的跳跃范围。因此,B使用其他石头作为中间站,通过一系列的小跳跃到达她。两块石头之间的青蛙距离被定义为两块石头之间所有可能路径上的最小必要跳跃距离,某条路径的必要跳跃距离即这条路径中单次跳跃的最远跳跃距离。计算A和B石头之间的青蛙距离。
题解:
对最短路算法修改,求局部最大和整体最小
分别使用迪杰斯特拉算法,Floyd算法和SPFA算法,结果如下
算法 | Time | Memory |
---|---|---|
Dijkstra | 235ms | 5428kB |
Floyd | 500ms | 5424kB |
SPFA | 282ms | 5388kB |
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Frogger {
public static void main(String[] args) {
resolve();
}
private static double INF = 0x3f3f3f3f;
private static double[][] g;
private static double[] minValue;
private static boolean[] visited;
private static int n;
private static void resolve() {
int k = 0;
Scanner scanner = new Scanner(System.in);
while (true) {
n = scanner.nextInt();
if (n == 0) {
break;
}
int[][] dots = new int[n + 1][2];
for (int i = 1; i <= n; i++) {
dots[i][0] = scanner.nextInt();
dots[i][1] = scanner.nextInt();
}
g = new double[n + 1][n + 1];
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
double x = Math.pow(dots[i][0] - dots[j][0], 2);
double y = Math.pow(dots[i][1] - dots[j][1], 2);
g[i][j] = Math.sqrt(x + y);
g[j][i] = g[i][j];
}
}
// DIJ();
// System.out.println("Scenario #" + (++k));
// System.out.printf("Frog Distance = %.3f\n\n", minValue[2]);
// Floyd();
// System.out.println("Scenario #" + (++k));
// System.out.printf("Frog Distance = %.3f\n\n", g[1][2]);
SPFA();
System.out.println("Scenario #" + (++k));
System.out.printf("Frog Distance = %.3f\n\n", minValue[2]);
}
}
private static void SPFA() {
minValue = new double[n + 1];
Arrays.fill(minValue, INF);
minValue[1] = 0;
visited = new boolean[n + 1];
visited[1] = true;
boolean isUpdated;
Queue<Integer> queue = new LinkedList<Integer>();
queue.offer(1);
while (!queue.isEmpty()) {
int now = queue.poll();
visited[now] = false;
for (int i = 1; i <= n; i++) {
if (Math.max(minValue[now], g[now][i]) < minValue[i]) {
minValue[i] = Math.max(minValue[now], g[now][i]);
if (!visited[i]) {
queue.offer(i);
visited[i] = true;
}
}
}
}
}
private static void Floyd() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= n; k++) {
double temp = Math.max(g[j][i], g[i][k]);
g[j][k] = temp < g[j][k] ? temp : g[j][k];
}
}
}
}
private static void DIJ() {
minValue = new double[n + 1];
visited = new boolean[n + 1];
visited[1] = true;
for (int i = 1; i <= n; i++) {
minValue[i] = g[1][i] > 0 ? g[1][i] : INF;
}
for (int i = 2; i < n; i++) {
int temp = 0;
double min = INF;
for (int j = 2; j <= n; j++) {
if (!visited[j] && min > minValue[j]) {
min = minValue[j];
temp = j;
}
}
visited[temp] = true;
for (int j = 1; j <= n; j++) {
minValue[j] = !visited[j] && g[temp][j] > 0 ? Math.min(Math.max(min, g[temp][j]), minValue[j])
: minValue[j];
}
}
}
}
3. Heavy Transportation原题链接
和上面那道题正好相反,求局部最短距离,求全局最大
这里使用了堆优化的DIJ和普通的DIJ,以及Bellman_Ford算法
BellMan_Ford算法超时。堆优化OJ因为是java1.5的,编译出错。。。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
public class Main {
private static ArrayList<int[]>[] edges;
private static int[] ans;
private static int n;
private static final int INF = 0x3f3f3f3f;
static class MyComparator implements Comparator<int[]> {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] == o2[0] ? -1 : o2[1] - o1[1];
}
}
public static void main(String[] args) {
resovle();
}
public static void resovle() {
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
int k = 0;
while (--i >= 0) {
n = scanner.nextInt();
int m = scanner.nextInt();
edges = new ArrayList[n + 1];
for (int j = 1; j <= n; j++) {
edges[j] = new ArrayList<int[]>();
}
for (int j = 0; j < m; j++) {
int start = scanner.nextInt();
int end = scanner.nextInt();
int w = scanner.nextInt();
edges[start].add(new int[] { end, w });
edges[end].add(new int[] { start, w });
}
Bellman_ford();
System.out.println("Scenario #" + (++k) + ":");
System.out.println(ans[n]);
System.out.println();
}
}
private static void Bellman_ford() {
ans = new int[n + 1];
Arrays.fill(ans, 0);
ans[1] = INF;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int[] edge : edges[j]) {
int minPath = Math.min(edge[1], ans[j]);
if (ans[edge[0]] < minPath) {
ans[edge[0]] = minPath;
}
}
}
}
}
private static void DIJ2() {
ans = new int[n + 1];
Arrays.fill(ans, 0);
ans[1] = INF;
boolean[] visited = new boolean[n + 1];
for (int i = 1; i < n; i++) {
int temp = 0;
int max = 0;
for (int j = 1; j <= n; j++) {
if (!visited[j] && max < ans[j]) {
max = ans[j];
temp = j;
}
}
visited[temp] = true;
for (int[] edge : edges[temp]) {
int pathMin = Math.min(edge[1], ans[temp]);
if (pathMin > ans[edge[0]]) {
ans[edge[0]] = pathMin;
}
}
}
}
private static void DIJ() {
ans = new int[n + 1];
Arrays.fill(ans, 0);
ans[1] = INF;
boolean[] visited = new boolean[n + 1];
Queue<int[]> queue = new PriorityQueue<int[]>(new MyComparator());
queue.add(new int[] { 1, ans[1] });
while (!queue.isEmpty()) {
int[] now = queue.poll();
if (visited[now[0]] == true) {
continue;
}
visited[now[0]] = true;
for (int[] edge : edges[now[0]]) {
int pathMin = Math.min(edge[1], ans[now[0]]);
if (ans[edge[0]] < pathMin) {
ans[edge[0]] = pathMin;
queue.offer(new int[] { edge[0], ans[edge[0]] });
}
}
}
}
}
1. Til the Cows Come Home原题链接
这道题就是一个求最短路问题。
这里我使用了Bellman-Ford算法
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
resolve();
}
private static int[][] g;
private static int n;
private static int t;
private static int[] min_pathLen;
private static boolean[] updated;
private static int count;
public static void resolve() {
Scanner scanner = new Scanner(System.in);
t = scanner.nextInt();
n = scanner.nextInt();
g = new int[n + 1][n + 1];
for (int i = 0; i < t; i++) {
int source = scanner.nextInt();
int end = scanner.nextInt();
int len = scanner.nextInt();
g[source][end] = g[source][end] == 0 ? len : Math.min(g[source][end], len);
g[end][source] = g[source][end];
}
updated = new boolean[n + 1];
min_pathLen = new int[n + 1];
Arrays.fill(min_pathLen, 200000);
Arrays.fill(updated, true);
count = n;
min_pathLen[1] = 0;
Bellman_ford();
System.out.println(min_pathLen[n]);
}
private static void Bellman_ford() {
while (true) {
boolean isUpdated = false;
for (int i = 1; i <= n; i++) {
if (updated[i]) {
for (int j = 1; j <= n; j++) {
if (g[i][j] > 0 && min_pathLen[i] + g[i][j] < min_pathLen[j]) {
min_pathLen[j] = min_pathLen[i] + g[i][j];
updated[j] = true;
isUpdated = true;
}
}
updated[i] = false;
}
}
if (!isUpdated) {
break;
}
}
}
}