Pagini recente » Cod sursa (job #1688377) | Cod sursa (job #2676620) | Cod sursa (job #1947221) | Cod sursa (job #1184336) | Cod sursa (job #2198427)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package laborator9pa;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;
/**
*
* @author maria
*/
public class FloydWarshall {
static class Task {
public static final String INPUT_FILE = "in";
public static final String OUTPUT_FILE = "out";
public static final int NMAX = 100005; // 10^5
int n;
int[][] d;
@SuppressWarnings("unchecked")
ArrayList<Integer> adj[] = new ArrayList[NMAX];
private void readInput() {
try {
Scanner sc = new Scanner(new BufferedReader(new FileReader(
"royfloyd.in")));
n = sc.nextInt();
d = new int[n+1][n+1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int x;
x = sc.nextInt();
d[i][j] = x;
}
}
sc.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void writeOutput(int[][] d) {
try {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(
"royfloyd.out")));
for (int i = 0; i <=n; i++) {
for (int j = 0; j <=n; j++) {
pw.printf("%d ", d[i][j]);
}
}
pw.printf("\n");
pw.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private int[][] getResult() {
// TODO: Faceti sortarea topologica a grafului stocat cu liste de
// adiacenta in adj.
// *******
// ATENTIE: nodurile sunt indexate de la 1 la n.
// *******
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i != j && d[i][j] == 0)
d[i][j] = Integer.MAX_VALUE;
}
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (d[i][k] + d[k][j] < d[i][j]) {
d[i][j] = d[i][k] + d[k][j];
}
}
}
}
System.out.println("iei");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(d[i][j] + " ");
}
System.out.println("");
}
return d;
}
public void solve() {
readInput();
writeOutput(getResult());
}
}
public static void main(String[] args) {
new Task().solve();
}
}