Cod sursa(job #3202403)

Utilizator miruna.abAbaianiti Miruna miruna.ab Data 11 februarie 2024 14:44:11
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
int n;
 int dist[101][101];
    int a[101][101], x, y, z, maxx, poz, m;
int inf=INT_MAX;
int main()
{
   fin>>n;
   for(int i=1;i<=n;++i)
        for(int j=1;j<=n;++j)fin>>a[i][j];
    for(int i=1;i<=n;++i){
        for(int j=1;j<=n;++j){
            if(i==j){
                dist[i][j]=0;
            }else if(a[i][j]!=0){
                dist[i][j]=a[i][j];
            }else{
                dist[i][j]=inf;
            }
        }
    }
    for(int k=1;k<=n;++k){
        for(int i=1;i<=n;++i){
            for(int j=1;j<=n;++j)if(dist[i][k]!=inf&&dist[k][j]!=inf){
            if(dist[i][j]>dist[i][k]+dist[k][j]){
                dist[i][j]=dist[i][k]+dist[k][j];
            }   
            }
        }
    }
   for(int i=1; i<=n; i++){
   for(int j=1; j<=n; j++)fout<<dist[i][j]<<' '; fout<<endl;}
    return 0;
}