Cod sursa(job #2428106)

Utilizator valentin11cCraciun Valentin-Gabriel valentin11c Data 3 iunie 2019 20:06:16
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 kb
#include <iostream>
#include<fstream>

using namespace std;
int NumberOfElements,PathMatrix[105][105];
void Read_Initial_Matrix()
{
    ifstream f("royfloyd.in");
    f>>NumberOfElements;
    for(int i=1;i<=NumberOfElements;i++)
    {
        for(int j=1;j<=NumberOfElements;j++)
        {
            f>>PathMatrix[i][j];
        }

    }

    f.close();
}
void Roy_Floyd()
{

    for(int k=1;k<=NumberOfElements;k++)
    {
        for(int i=1;i<=NumberOfElements;i++)
        {
            for(int j=1;j<=NumberOfElements;j++)
            {
               if((PathMatrix[i][k]!=0 && PathMatrix[k][j]!=0 && i!=j))
                   {
                       if(PathMatrix[i][j])
                        PathMatrix[i][j]=min(PathMatrix[i][j],PathMatrix[i][k]+PathMatrix[k][j]);
                        else
                        PathMatrix[i][j]=PathMatrix[i][k]+PathMatrix[k][j];
                   }
            }
        }

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

    }
    g.close();
}


int main()
{

    Read_Initial_Matrix();
    Roy_Floyd();
    Write_Path_Matrix();
    return 0;
}