Cod sursa(job #2919381)

Utilizator MihaiVIIIIlinca Mihai MihaiVIII Data 17 august 2022 11:15:46
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <iostream>
#include <fstream>

using namespace std;

void royfloyd(int n,int a[100][100]) 
{
    for (int k = 0; k < n; k++) 
    {
        for (int i = 0; i < n; i++) 
        {
            for (int j = 0; j < n; j++) 
            {
                a[i][j] = min(a[i][j],a[i][k] + a[k][j]);
            }
        }
    }
}

int main()
{
    ifstream in("royfloyd.in");
    ofstream out("royfloyd.out");
    int n,a[100][100];
    in >> n;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            in >> a[i][j];
            if (a[i][j] == 0 && i != j)
            {
                a[i][j] = 1000000;
            }
            
        }    
    }
    royfloyd(n,a);
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (a[i][j] == 1000000)
            {
                out << "0 ";
            }
            else
            {
                out << a[i][j] <<" ";
            }
        }
        out << "\n";    
    }
    in.close();
    out.close();
    return 0;
}