Cod sursa(job #1528428)

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

using namespace std;

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

int N, A[Nmax][Nmax], DM[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];
         if((A[i][j] == 0) && (i != j))
         {
            A[i][j] = INF;
         }
         DM[i][j] = 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 ++)
         {
            DM[i][j] = min(DM[i][j], DM[i][k]+DM[k][j]);
         }
      }
   }
}

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

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