Cod sursa(job #1446052)

Utilizator Burbon13Burbon13 Burbon13 Data 31 mai 2015 19:40:34
Problema Floyd-Warshall/Roy-Floyd Scor 10
Compilator java Status done
Runda Arhiva educationala Marime 1.02 kb
import java.io.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        int[][] mat = new int[102][102];
        int n;
        Scanner reader = new Scanner(new FileInputStream("royfloyd.in"));
        n = reader.nextInt();
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= n; ++j)
                mat[i][j] = reader.nextInt();
        reader.close();
        for(int k = 1; k <= n; ++k)
            for(int i = 1; i <= n; ++i)
                for(int j = 1; j <= n; ++j)
                    if(i != j && mat[i][k] != 0 && mat[k][j] != 0 && (mat[i][j] == 0 || mat[i][j] > mat[i][k] + mat[k][j]))
                        mat[i][j] = mat[i][k] + mat[k][j];
        PrintWriter writer = new PrintWriter("royfloyd.out");
        for(int i = 1; i <= n; ++i){
            for(int j = 1; j <= n; ++j)
                writer.write(mat[i][j] + " ");
            writer.write("\n");
        }
        writer.close();
    }
    
}