Cod sursa(job #2220427)

Utilizator amimunAmelia Munteanu amimun Data 11 iulie 2018 17:35:22
Problema Floyd-Warshall/Roy-Floyd Scor 80
Compilator java Status done
Runda Arhiva educationala Marime 1.73 kb
import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        int n, i, j, k;
        String line;

	    try {
            BufferedReader br = new BufferedReader(new FileReader("royfloyd.in"));
            PrintWriter writer = new PrintWriter("royfloyd.out");
            line = br.readLine();
            n = Integer.parseInt(line);
            int[][] a = new int[n+1][n+1];
            int[][] dist = new int[n+1][n+1];
            StringTokenizer st;

            for (i = 1; i <= n; i++) {
                line = br.readLine();
                st = new StringTokenizer(line, " ");
                for (j = 1; j <= n; j++) {
                    a[i][j] = Integer.parseInt(st.nextToken());
                    if (a[i][j] == 0 && (i != j)) {
                        dist[i][j] = Integer.MAX_VALUE / 2;
                    } else if (i == j) {
                        dist[i][j] = 0;
                    } else {
                        dist[i][j] = a[i][j];
                    }
                }
            }

            for (k = 1; k <= n; k++) {
                for (i = 1; i <= n; i++) {
                    for (j = 1; j <= n; j++) {
                        if (dist[i][j] > dist[i][k] + dist[k][j])
                            dist[i][j] = dist[i][k] + dist[k][j];
                    }
                }
            }

            for (i = 1; i <= n; i++) {
                for (j = 1; j <= n; j++) {
                    writer.write(String.valueOf(dist[i][j]) + ((j != n ) ? " " : ""));
                }
                writer.println();
            }

            br.close();
            writer.close();
        } catch (Exception e) {

        }
    }
}