Cod sursa(job #2075984)

Utilizator cristicretancristi cretan cristicretan Data 25 noiembrie 2017 21:49:50
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <iostream>
#include <fstream>
#include <utility>
#include <algorithm>
#include <sstream>
#include <memory>
#define NMax 101
using namespace std;

ifstream f("royfloyd.in");
ofstream g("royfloyd.out");

int dist[NMax][NMax], n, a[NMax][NMax];

int main()
{
    f >> n;
    for(int i = 0; i < n; ++i)
        for(int j = 0; j < n; ++j)
        {
            f >> a[i][j];
            dist[i][j] = a[i][j];
        }
    for(int k = 0; k < n; ++k)
        for(int i = 0; i < n; ++i)
            for(int j = 0; j < n; ++j)
                if(dist[i][k] && dist[k][j] && (dist[i][j] > dist[i][k] + dist[k][j] || !dist[i][j]) && i != j) dist[i][j] = dist[i][k] + dist[k][j]; /// daca distanta de la i la j e mai mare decat distanta de la i la k + distanta de la k la j atunci actualizam...
    for(int i = 0; i < n; ++i)
    {
        for(int j = 0; j < n; ++j)
            g << dist[i][j] << " ";
        g << '\n';
    }
    return 0;
}