Cod sursa(job #2432666)

Utilizator melutMelut Zaid melut Data 24 iunie 2019 17:40:28
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <fstream>
#include <vector>
#include <string>

using namespace std;


string const inFile = "royfloyd.in";
string const outFile = "royfloyd.out";


ifstream Read(inFile);
ofstream Write(outFile);


int main() {
    unsigned n;
    vector<vector<int>> dist;
    unsigned i, j, k;

    Read >> n;

    dist.resize(n, vector<int>(n));

    for (i = 0; i < n; ++i)
    for (j = 0; j < n; ++j) {
        Read >> dist[i][j];
    }

    for (k = 0; k < n; ++k)
    for (i = 0; i < n; ++i)
    for (j = 0; j < n; ++j) {
        if (i != j)
        if (dist[i][k] != 0)
        if (dist[k][j] != 0) {
            if (dist[i][j] == 0) {
                dist[i][j] = dist[i][k] + dist[k][j];
            }
            else if (dist[i][k] + dist[k][j] < dist[i][j]) {
                dist[i][j] = dist[i][k] + dist[k][j];
            }
        }
    }

    for (i = 0; i < n; ++i) {
        for (j = 0; j < n; ++j) {
            Write << dist[i][j] << ' ';
        }

        Write << '\n';
    }

    return 0;
}