Cod sursa(job #2198379)

Utilizator ioanamoraru14Ioana Moraru ioanamoraru14 Data 24 aprilie 2018 13:15:17
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

const int inf = 9999999;
const int kmax = 105;

class Task {
 public:
	void solve() {
		read_input();
        get_result();
		print_output();
	}

 private:
	int n;
    int mat[kmax][kmax];

	void read_input() {
		ifstream fin("royfloyd.in");
		fin >> n;

        int x;
		for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                fin >> x;
                if (i != j && x == 0) {
                    mat[i][j] = inf;
                }
                else {
                    mat[i][j] = x;
                }
            }
		}
		fin.close();
	}

	void get_result() {
        for (int k = 1; k <= n; k++) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    mat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j]);
                }
            }
        }
	}

	void print_output() {
		ofstream fout("royfloyd.out");
		for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
			    fout << mat[i][j] << ' ';
            }
		    fout << '\n';
		}
		fout.close();
	}
};

int main() {
	Task *task = new Task();
	task->solve();
	delete task;
	return 0;
}