Cod sursa(job #3360424)

Utilizator chisianisChis Ianis-Alexandru chisianis Data 13 iulie 2026 17:20:19
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

ifstream fin ("royfloyd.in");
ofstream fout ("royfloyd.out");

const int INF = 1e8;
int adj[110][110];

int main()
{
    FASTIO
    
    int n; fin >> n;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            fin >> adj[i][j];
    vector<vector<int>> dist(n + 1, vector<int>(n + 1));
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++){
            if(i != j && adj[i][j] == 0) dist[i][j] = INF;
            else dist[i][j] = adj[i][j];
        }
    for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                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 << '\n';
    }
        
    return 0;
}