Cod sursa(job #3260291)

Utilizator andreea0146Nicula Andreea andreea0146 Data 1 decembrie 2024 14:32:35
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <iostream>
#include<fstream>
using namespace std;
const int inf=9999999;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");

int n;
int c[101][101];

void init()
{
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            if(c[i][j]==0&&i!=j)
                c[i][j]=inf;
}

void citire()
{
    int x,y,cc;
    fin>>n;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            fin>>c[i][j];
    init();
}

void Roy_Floyd()
{
    int cost;
    for(int k=1; k<=n; k++)
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            {
                cost=c[i][k]+c[k][j];
                if(cost<c[i][j]) c[i][j]=cost;
            }
}

void afis()
{
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
            if(c[i][j]==inf)
                fout<<"0 ";
            else
                fout<<c[i][j]<<' ';
        fout<<'\n';
    }
}

int main()
{
    citire();
    Roy_Floyd();
    afis();

    fin.close();
    fout.close();
    return 0;
}