Pagini recente » Cod sursa (job #2529822) | Cod sursa (job #121223) | Cod sursa (job #2670692) | Cod sursa (job #2759272) | Cod sursa (job #1357298)
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Main {
public static short[][] readInput() throws FileNotFoundException, IOException {
FileInputStream fs = new FileInputStream("royfloyd.in");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
byte vertexCount = Byte.parseByte(br.readLine());
short[][] matrix = new short[vertexCount][vertexCount];
for (byte i = 0; i < vertexCount; i++) {
String[] parts = br.readLine().split(" ");
for (byte j = 0; j < vertexCount; j++) {
matrix[i][j] = Short.parseShort(parts[j]);
}
}
br.close();
fs.close();
return matrix;
}
public static void writeOutput(short[][] matrix) throws FileNotFoundException {
PrintWriter pr = new PrintWriter("royfloyd.out");
for (byte i = 0; i < (byte) matrix[0].length; i++) {
for (byte j = 0; j < (byte) matrix[0].length; j++) {
pr.write(matrix[i][j] + " ");
}
pr.println();
}
pr.close();
}
public static void main(String[] args) throws IOException {
short[][] dist = readInput();
for (byte k = 0; k < (byte) dist[0].length; k++) {
for (byte i = 0; i < (byte) dist[0].length; i++) {
for (byte j = 0; j < (byte) dist[0].length; j++) {
if (i != j && dist[i][k] != 0 && dist[k][j] != 0 && dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = (short) (dist[i][k] + dist[k][j]);
}
}
}
}
writeOutput(dist);
}
}