Cod sursa(job #3178438)

Utilizator AlexInfoIordachioaiei Alex AlexInfo Data 1 decembrie 2023 18:05:18
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <bits/stdc++.h>

using namespace std;

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

using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define fi first
#define se second

const int NMAX = 105;
const int INF = 0x3f3f3f3f;

int n, a[NMAX][NMAX], dist[NMAX][NMAX];

void read()
{
    in >> n;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) {
            in >> dist[i][j];
            if (dist[i][j]==0 && i!=j)
                dist[i][j] = INF;
        }
    
}

void FloydWarshall()
{

    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];
}

void solve()
{
    FloydWarshall();
    for (int i = 1; i <= n; i++, out << '\n')
        for (int j = 1; j <= n; j++)
            out << dist[i][j] << ' ';
}

int main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);

    read();
    solve();

    return 0;
}