Cod sursa(job #2964902)

Utilizator Emmy432622Rotariu Emanuel Emmy432622 Data 14 ianuarie 2023 09:33:16
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#include <bits/stdc++.h>

using namespace std;

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

const int Mx = 1000000000;
int d[105][105];
int n;

int main()
{
    fin >> n;

    for(int i = 1 ; i <= n ; ++i)
        for(int j = 1 ; j <= n ; ++j)
        {
            fin >> d[i][j];
            if(!d[i][j])
              d[i][j] = Mx;
        }

    for(int z = 1 ; z <= n ; ++z)
        for(int x = 1 ; x <= n ; ++x)
            for(int y = 1 ; y <= n ; ++y)
                if(x!=y && d[x][z] != Mx && d[z][y] != Mx)
                  d[x][y] = min(d[x][y],d[x][z]+d[z][y]);

    for(int i = 1 ; i <= n ; ++i)
    {
      for(int j = 1 ; j <= n ; ++j)
        {
          if(d[i][j] >= Mx)
            fout << 0;
          else
            fout << d[i][j];
          fout << ' ';
        }
      fout << '\n';
    }

    return 0;
}