Cod sursa(job #2239566)

Utilizator AndreiVisoiuAndrei Visoiu AndreiVisoiu Data 11 septembrie 2018 10:09:27
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int INF = 1 << 20;
int D[101][101];
int n, m;

void Roy_Floyd() {
    for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++) {
                int cost = D[i][k] + D[k][j];
                if(D[i][j] > cost)
                    D[i][j] = cost;
            }
}
int main()
{
    int x;
    in >> n;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++) {
            in >> x;
            if(x == 0 && i != j)
                D[i][j] = INF;
            else D[i][j] = x;
        }
    Roy_Floyd();
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++)
            if(D[i][j] == INF) out << "0 ";
            else out << D[i][j] << ' ';
        out << '\n';
    }
    return 0;
}