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.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;
        }

        try (Scanner scanner = new Scanner(new BufferedReader(new FileReader(args[0])))) {
            createFile();

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                if (line.startsWith("Problem")) {
                    // Extract number of vertices from the line
                    int n = Integer.parseInt(line.split("n = ")[1].trim());
                    if (n < 5 || n > 10) {
                        throw new IllegalArgumentException("Invalid number of vertices.");
                    }

                    // Read and initialize the adjacency matrix for the current problem
                    initializeMatrices(n, scanner);
                    
                    // Compute shortest paths and print results
                    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) {
            if (!scanner.hasNextLine()) {
                throw new IllegalStateException("Expected adjacency matrix row is missing.");
            }
            String[] values = scanner.nextLine().trim().split("\s+");
            for (int j = 0; j < n; ++j) {
                int weight = Integer.parseInt(values[j]);
                adjMatrix[i][j] = weight;
                if (weight < 0 || weight > 10) {
                    throw new IllegalArgumentException("Edge weight must be between 0 and 10.");
                }
                if (i == j) {
                    adjMatrix[i][j] = 0;
                } else if (weight != 0) {
                    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 from, int to) {
        return pMatrix[from][to] == to && adjMatrix[from][to] > 0;
    }
    
    private static String getShortestPathAsString(int source, int destination) {
        if (adjMatrix[source][destination] == Integer.MAX_VALUE) {
            return "No path";
        }
        StringBuilder pathBuilder = new StringBuilder();
        pathBuilder.append("V").append((char) ('1' + source));
        int inter = source;
        while (inter != destination) {
            inter = pMatrix[inter][destination];
            pathBuilder.append(" V").append((char) ('1' + inter));
        }
        return pathBuilder.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();
        }
    }
} The program is giving the output as Problem1: n = 7
Pmatrix:
 0   6   5   4   6   3   6 

VA - VB: Shortest Path = Directly Connected
VA - VC: Shortest Path = Directly Connected
VA - VD: Shortest Path = Directly Connected
VA - VE: Shortest Path = Directly Connected
VA - VF: Shortest Path = Directly Connected
VA - VG: Shortest Path = Directly Connected



Problem2: n = 7
Pmatrix:
 6   0   6   4   5   5   3 

VB - VA: Shortest Path = Directly Connected
VB - VC: Shortest Path = Directly Connected
VB - VD: Shortest Path = Directly Connected
VB - VE: Shortest Path = Directly Connected
VB - VF: Shortest Path = Directly Connected
VB - VG: Shortest Path = Directly Connected



Problem3: n = 7
Pmatrix:
 5   6   0   3   1   4   6 

VC - VA: Shortest Path = Directly Connected
VC - VB: Shortest Path = Directly Connected
VC - VD: Shortest Path = Directly Connected
VC - VE: Shortest Path = Directly Connected
VC - VF: Shortest Path = Directly Connected
VC - VG: Shortest Path = Directly Connected



Problem4: n = 7
Pmatrix:
 4   4   3   0   4   1   4 

VD - VA: Shortest Path = Directly Connected
VD - VB: Shortest Path = Directly Connected
VD - VC: Shortest Path = Directly Connected
VD - VE: Shortest Path = Directly Connected
VD - VF: Shortest Path = Directly Connected
VD - VG: Shortest Path = Directly Connected



Problem5: n = 7
Pmatrix:
 6   5   1   4   0   5   5 

VE - VA: Shortest Path = Directly Connected
VE - VB: Shortest Path = Directly Connected
VE - VC: Shortest Path = Directly Connected
VE - VD: Shortest Path = Directly Connected
VE - VF: Shortest Path = Directly Connected
VE - VG: Shortest Path = Directly Connected



Problem6: n = 7
Pmatrix:
 3   5   4   1   5   0   3 

VF - VA: Shortest Path = Directly Connected
VF - VB: Shortest Path = Directly Connected
VF - VC: Shortest Path = Directly Connected
VF - VD: Shortest Path = Directly Connected
VF - VE: Shortest Path = Directly Connected
VF - VG: Shortest Path = Directly Connected



Problem7: n = 7
Pmatrix:
 6   3   6   4   5   3   0 

VG - VA: Shortest Path = Directly Connected
VG - VB: Shortest Path = Directly Connected
VG - VC: Shortest Path = Directly Connected
VG - VD: Shortest Path = Directly Connected
VG - VE: Shortest Path = Directly Connected
VG - VF: Shortest Path = Directly Connected



Problem1: n = 6
Pmatrix:
 0   1   2   1   3   4 

VA - VB: Shortest Path = Directly Connected
VA - VC: Shortest Path = Directly Connected
VA - VD: Shortest Path = Directly Connected
VA - VE: Shortest Path = Directly Connected
VA - VF: Shortest Path = Directly Connected



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

VB - VA: Shortest Path = Directly Connected
VB - VC: Shortest Path = Directly Connected
VB - VD: Shortest Path = Directly Connected
VB - VE: Shortest Path = Directly Connected
VB - VF: Shortest Path = Directly Connected



Problem3: n = 6
Pmatrix:
 2   3   0   3   3   6 

VC - VA: Shortest Path = Directly Connected
VC - VB: Shortest Path = Directly Connected
VC - VD: Shortest Path = Directly Connected
VC - VE: Shortest Path = Directly Connected
VC - VF: Shortest Path = Directly Connected



Problem4: n = 6
Pmatrix:
 1   2   3   0   3   5 

VD - VA: Shortest Path = Directly Connected
VD - VB: Shortest Path = Directly Connected
VD - VC: Shortest Path = Directly Connected
VD - VE: Shortest Path = Directly Connected
VD - VF: Shortest Path = Directly Connected



Problem5: n = 6
Pmatrix:
 3   2   3   3   0   5 

VE - VA: Shortest Path = Directly Connected
VE - VB: Shortest Path = Directly Connected
VE - VC: Shortest Path = Directly Connected
VE - VD: Shortest Path = Directly Connected
VE - VF: Shortest Path = Directly Connected



Problem6: n = 6
Pmatrix:
 4   3   6   5   5   0 

VF - VA: Shortest Path = Directly Connected
VF - VB: Shortest Path = Directly Connected
VF - VC: Shortest Path = Directly Connected
VF - VD: Shortest Path = Directly Connected
VF - VE: Shortest Path = Directly Connected

but the 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 

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: Make necessary changes to the code to get the expected output