Cod sursa(job #1321409)

Utilizator rockerboyHutter Vince rockerboy Data 19 ianuarie 2015 08:28:28
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>
#include <vector>

std::ifstream in ("royfloyd.in");
std::ofstream out("royfloyd.out");

int n;
std::vector< std::vector<int> > x;

int main()
{
    in >> n;
    x.resize (n+1, std::vector<int>(n+1));

    int i, j, k;
    for (i=1; i<=n; i++)
        for (j=1; j<=n; j++)
            in >> x[i][j];

    for (k=1; k<=n; k++)
        for (i=1; i<=n; i++)
            for (j=1; j<=n; j++)
                if (!x[i][k]) continue;
                else if (!x[k][j]) continue;
                else if (i == j) continue;
                else if (!x[i][j] || x[i][j] > x[i][k]+x[k][j]) x[i][j] = x[i][k]+x[k][j];

    for (i=1; i<=n; i++) {
        for (j=1; j<=n; j++) out << x[i][j] << " ";
        out << "\n";
    }

}