Cod sursa(job #2425835)

Utilizator gabrielmGabriel Majeri gabrielm Data 25 mai 2019 08:01:58
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.24 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

#define INFTY 999999

int main() {
    ifstream in("royfloyd.in");

    int n;
    in >> n;

    vector<vector<int>> mat(n, vector<int>(n));

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int x;
            in >> x;

            if (i == j) {
                mat[i][j] = 0;
            } else if (x == 0) {
                mat[i][j] = INFTY;
            } else {
                mat[i][j] = x;
            }
        }
    }


    for (int k = 0; k < n; ++k) {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (i == j) {
                    continue;
                }

                int& cost_vechi = mat[i][j];
                int cost_nou = mat[i][k] + mat[k][j];
                if (cost_nou < cost_vechi) {
                    cost_vechi = cost_nou;
                }
            }
        }
    }

    ofstream out("royfloyd.out");

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (mat[i][j] == INFTY) {
                out << 0 << ' ';
            } else {
                out << mat[i][j] << ' ';
            }
        }
        out << '\n';
    }
}