Pagini recente » Cod sursa (job #2439989) | Cod sursa (job #1606151) | Cod sursa (job #1737800) | Cod sursa (job #1841981) | Cod sursa (job #3355271)
import java.io.*;
import java.util.StringTokenizer;
public class Main {
static class MyScanner {
private BufferedReader br;
private StringTokenizer st;
public MyScanner(Reader reader) {
br = new BufferedReader(reader);
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) throws IOException {
MyScanner scanner = new MyScanner(new FileReader("royfloyd.in"));
int n = scanner.nextInt();
int[][] dist = new int[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
dist[i][j] = scanner.nextInt();
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i != j && dist[i][j] == 0)
{
dist[i][j] = 100000;
}
}
}
for (int k = 0; k < n; k++)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
try (BufferedWriter writer = new BufferedWriter(new FileWriter("royfloyd.out"))) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dist[i][j] == 100000)
dist[i][j] = 0;
writer.write(dist[i][j] + " ");
}
writer.newLine();
}
}
}
}