import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class floyd {
    private static int[][] adjMatrix;
    private static int[][] pMatrix;
    private static final String OUTPUT_FILE = "output_floyd.txt";
    private static ArrayList<Integer> path = new ArrayList<>();

    public static void main(String[] args) throws IOException {
        if (args.length != 1) {
            System.err.println("Usage: java Floyd <graph-file>");
            return;
        }
s
        try (Scanner scanner = new Scanner(new BufferedReader(new FileReader(args[0])))) {
            createFile();

            while (scanner.hasNextInt()) {
                int n = scanner.nextInt(); // Number of vertices

                if (n < 5 || n > 10) {
                    throw new IllegalArgumentException("Invalid number of vertices.");
                }

                initializeMatrices(n, scanner);
                calculateShortestPaths();
                printResult(n);
            }
        }
    }

    private static void createFile() throws FileNotFoundException {
        PrintWriter writer = new PrintWriter(OUTPUT_FILE);
        writer.close();
    }

    private static void initializeMatrices(int n, Scanner scanner) {
        adjMatrix = new int[n][n];
        pMatrix = new int[n][n];

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                adjMatrix[i][j] = scanner.nextInt();
                if (adjMatrix[i][j] < 0 || adjMatrix[i][j] > 10) {
                    throw new IllegalArgumentException("Edge weight must be between 0 and 10.");
                }
                if (i == j) {
                    adjMatrix[i][j] = 0;
                } else {
                    pMatrix[i][j] = j;
                }
            }
        }
    }

    private static void calculateShortestPaths() {
        int n = adjMatrix.length;

        for (int k = 0; k < n; ++k) {
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < n; ++j) {
                    if (adjMatrix[i][k] + adjMatrix[k][j] < adjMatrix[i][j]) {
                        adjMatrix[i][j] = adjMatrix[i][k] + adjMatrix[k][j];
                        pMatrix[i][j] = pMatrix[i][k];
                    }
                }
            }
        }
    }

    private static void printResult(int n) throws FileNotFoundException {
        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < n; ++i) {
            clearPathList();
            builder.setLength(0);
            builder.append("Problem")
                    .append(i + 1)
                    .append(": n = ")
                    .append(n)
                    .append('\n')
                    .append("Pmatrix:\n");

            for (int j = 0; j < n; ++j) {
                builder.append(formatElement(adjMatrix[i][j]));
                if (j != n - 1) {
                    builder.append(' ');
                }
            }
            builder.append('\n');

            appendToFile(builder.toString());

            builder.setLength(0);
            for (int j = 0; j < n; ++j) {
                if (i == j) continue;
                buildPath(i, j);

                builder
                        .append("V")
                        .append((char) ('A' + i))
                        .append(" - V")
                        .append((char) ('A' + j))
                        .append(": Shortest Path = ");

                if (isDirectlyConnected(i, j)) {
                    builder.append("Directly Connected");
                } else {
                    builder
                            .append("[")
                            .append(getShortestPathAsString(i, j))
                            .append("]; Length = ")
                            .append(adjMatrix[i][j])
                            .append('\n');
                }

                appendToFile(builder.toString());
                builder.setLength(0);
            }
            builder.append('\n').append('\n');
            appendToFile(builder.toString());
        }
    }

    private static boolean isDirectlyConnected(int current, int destination) {
        return current == destination && adjMatrix[current][destination] > 0;
    }

    private static String getShortestPathAsString(int current, int destination) {
        StringBuilder stringBuilder = new StringBuilder();
        int intermediateNode = current;

        while (intermediateNode != destination) {
            stringBuilder.append((char) ('A' + intermediateNode));
            intermediateNode = pMatrix[intermediateNode][destination];
            if (intermediateNode != destination) {
                stringBuilder.append(" -> ");
            }
        }

        stringBuilder.append((char) ('A' + intermediateNode));
        return stringBuilder.toString();
    }

    private static void buildPath(int current, int destination) {
        path.clear();
        if (current != destination) {
            path.add(current);
            while (pMatrix[current][destination] != destination) {
                current = pMatrix[current][destination];
                path.add(current);
            }
            path.add(destination);
        }
    }

    private static void clearPathList() {
        path.clear();
    }

    private static String formatElement(int element) {
        return "%2d ".formatted(element);
    }

    private static void appendToFile(String string) throws FileNotFoundException {
        File outputFile = new File(OUTPUT_FILE);
        try (FileWriter fw = new FileWriter(outputFile, true);
             BufferedWriter bw = new BufferedWriter(fw);
             PrintWriter out = new PrintWriter(bw)) {

            out.println(string);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
} I want this program to read input form the graphfile.txt as 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 and give output in output.txt as 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 

V1-Vj: shortest path and length
V1 V1: 0
V1 V2: 6
V1 V3: 5
V1 V6 V4: 4
V1 V3 V5: 6
V1 V6: 3
V1 V6 V7: 6

V2-Vj: shortest path and length
V2 V1: 6
V2 V2: 0
V2 V5 V3: 6
V2 V4: 4
V2 V5: 5
V2 V4 V6: 5
V2 V7: 3

V3-Vj: shortest path and length
V3 V1: 5
V3 V5 V2: 6
V3 V3: 0
V3 V4: 3
V3 V5: 1
V3 V6: 4
V3 V5 V7: 6

V4-Vj: shortest path and length
V4 V6 V1: 4
V4 V2: 4
V4 V3: 3
V4 V4: 0
V4 V3 V5: 4
V4 V6: 1
V4 V6 V7: 4

V5-Vj: shortest path and length
V5 V3 V1: 6
V5 V2: 5
V5 V3: 1
V5 V3 V4: 4
V5 V5: 0
V5 V3 V6: 5
V5 V7: 5

V6-Vj: shortest path and length
V6 V1: 3
V6 V4 V2: 5
V6 V3: 4
V6 V4: 1
V6 V3 V5: 5
V6 V6: 0
V6 V7: 3

V7-Vj: shortest path and length
V7 V6 V1: 6
V7 V2: 3
V7 V5 V3: 6
V7 V6 V4: 4
V7 V5: 5
V7 V6: 3
V7 V7: 0

Problem2: n = 6
Pmatrix:
0  0  0  0  2  2  
0  0  1  1  0  0  
0  1  0  1  0  2  
0  1  1  0  0  2  
2  0  0  0  0  2  
2  0  2  2  2  0  

V1-Vj: shortest path and length
V1 V1: 0
V1 V2: 1
V1 V3: 2
V1 V4: 1
V1 V2 V5: 3
V1 V2 V6: 4

V2-Vj: shortest path and length
V2 V1: 1
V2 V2: 0
V2 V1 V3: 3
V2 V1 V4: 2
V2 V5: 2
V2 V6: 3

V3-Vj: shortest path and length
V3 V1: 2
V3 V1 V2: 3
V3 V3: 0
V3 V1 V4: 3
V3 V5: 3
V3 V1 V2 V6: 6

V4-Vj: shortest path and length
V4 V1: 1
V4 V1 V2: 2
V4 V1 V3: 3
V4 V4: 0
V4 V5: 3
V4 V1 V2 V6: 5

V5-Vj: shortest path and length
V5 V2 V1: 3
V5 V2: 2
V5 V3: 3
V5 V4: 3
V5 V5: 0
V5 V2 V6: 5

V6-Vj: shortest path and length
V6 V2 V1: 4
V6 V2: 3
V6 V2 V1 V3: 6
V6 V2 V1 V4: 5
V6 V2 V5: 5
V6 V6: 0