import java.io.*; import java.util.Scanner; public class floyd { public static final int…
- User
import java.io.*; import java.util.Scanner;
public class floyd {
public static final int INF = Integer.MAX_VALUE / 2;
// Function to reconstruct and print the shortest path from u to v private static void printPath(int u, int v, int[][] next, PrintWriter writer) { if (next[u][v] == -1) { writer.println(“No path”); return; } StringBuilder path = new StringBuilder(“V” + (u + 1)); while (u != v) { u = next[u][v]; path.append(" V").append(u + 1); } writer.print(path.toString()); }
public static void main(String[] args) throws FileNotFoundException { if (args.length != 1) { System.err.println(“Usage: Floyd ”); return; }
String inputFile = args[0]; Scanner scanner = new Scanner(new File(inputFile)); PrintWriter writer = new PrintWriter(“output.txt”);
while (scanner.hasNextLine()) { String line = scanner.nextLine().trim(); if (line.startsWith(“Problem”) && line.contains(“n =”)) { String problemId = line.split(" “)[1]; // Corrected to store the ID correctly int n = Integer.parseInt(line.split(“n =”)[1].trim());
int[][] dist = new int[n][n]; int[][] next = new int[n][n];
for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) dist[i][j] = 0; else dist[i][j] = INF; next[i][j] = -1; } }
for (int i = 0; i < n; i++) { if (scanner.hasNextLine()) { line = scanner.nextLine().trim(); String[] parts = line.split(”\s+“); for (int j = 0; j < n; j++) { int val = Integer.parseInt(parts[j]); if (val != 0) { dist[i][j] = val; next[i][j] = j; } } } }
for (int k = 0; k < n; ++k) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (dist[i][k] < INF && dist[k][j] < INF && dist[i][j] > dist[i][k] + dist[k][j]) { dist[i][j] = dist[i][k] + dist[k][j]; next[i][j] = next[i][k]; } } } }
writer.println(problemId + “: n = " + n);
// Printing paths and distances for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j && dist[i][j] != INF) { writer.print(problemId + " V” + (i + 1) + “-V” + (j + 1) + “: shortest path and length\n”); printPath(i, j, next, writer); writer.println(”: " + dist[i][j]); } } } } } scanner.close(); writer.close(); } }
Input is
Problem1 Amatrix: n = 7 0 6 5 4 6 3 6 6 0 6 4 5 5 3 5 6 0 3 1 4 6 4 4 3 0 4 1 4 6 5 1 4 0 5 5 3 5 4 1 5 0 3 6 3 6 4 5 3 0
Problem2 Amatrix: n = 6 0 1 2 1 3 4 1 0 3 2 2 3 2 3 0 3 3 6 1 2 3 0 3 5 3 2 3 3 0 5 4 3 6 5 5 0
graphfile.txt is the name of a file that includes input. The linesthat correspond to problem j will contains an integer n (between 5 and 10) that indicates how many cities and n x n adjacency matrix A (that is, the distance between n cities, between 1 to 10), in the next n rows. Note that no infinity will appear in the matrix A.
The correct and expected output is Problem1: n = 7 Pmatrix: 0 0 0 6 3 0 6 0 0 5 0 0 4 0 0 5 0 0 0 0 5 6 0 0 0 3 0 6 3 0 0 3 0 3 0 0 4 0 0 3 0 0 6 0 5 6 0 0 0
- Assistant
- User
Correct the code to get the expected output
- Assistant
- User
- Assistant
The error you're encountering is related to a misunderstanding in your import statements. The exception
FileNotFoundExceptionand the entire I/O functionality in Java are part of thejava.iopackage, which appears to be incorrectly imported in your code snippet. This mistake can cause compilation errors since the compiler cannot find references to the necessary classes.In Java, when you want to use classes like
File,PrintWriter,FileNotFoundException, etc., you need to import them properly from thejava.iopackage. The correct way to import these classes would either be by importing each one individually or by using a wildcard import for the entirejava.iopackage.