Cod sursa(job #1446276)

Utilizator Burbon13Burbon13 Burbon13 Data 1 iunie 2015 10:53:18
Problema Floyd-Warshall/Roy-Floyd Scor 10
Compilator java Status done
Runda Arhiva educationala Marime 1.03 kb
import java.io.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        short[][] mat = new short[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.nextShort();
        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] = (short) (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();
    }
    
}