Cod sursa(job #1528441)

Utilizator tc_iuresiures tudor-cristian tc_iures Data 19 noiembrie 2015 18:23:55
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
#include <iostream>
#include <fstream>

using namespace std;

const int INF  = 2000000000;
const int Nmax = 105;

int N, A[Nmax][Nmax];

void read()
{
   ifstream f("royfloyd.in");
   f >> N;
   for(int i = 1; i <= N; i ++)
   {
      for(int j = 1; j <= N; j ++)
      {
         f >> A[i][j];
      }
   }
   f.close();
}

void RoyFloyd()
{
   //I initialize the minimal cost matrix when I read the original one
   for(int k = 1; k <= N; k ++)
   {
      for(int i = 1; i <= N; i ++)
      {
         for(int j = 1; j <= N; j ++)
         {
            if(A[i][k] && A[k][j] && ((A[i][j] > A[i][k]+A[k][j]) || !A[i][j]) && (i != j))
            {
               A[i][j] = A[i][k] + A[k][j];
            }
         }
      }
   }
}

void print()
{
   ofstream g("royfloyd.out");
   for(int i = 1; i <= N; i ++)
   {
      for(int j = 1; j <= N; j ++)
      {
         g << A[i][j] << " ";
      }
      g << "\n";
   }
}

int main()
{
    read();
    RoyFloyd();
    print();
    return 0;
}