Cod sursa(job #3215902)

Utilizator AlexInfoIordachioaiei Alex AlexInfo Data 15 martie 2024 14:06:24
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <bits/stdc++.h>

using namespace std;

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

#define pii pair<int, int>
#define pb push_back
#define fi first
#define se second

const int NMAX = 1e5 + 30;
const int INF = 0x3f3f3f3f;

int n, m[105][105], rfw[105][105];

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

void RFW()
{
    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                if (i!=j) rfw[i][j] = min(rfw[i][j], rfw[i][k] + rfw[k][j]);
}

void solve()
{
    RFW();
    for (int i = 1; i <= n; i++, out << '\n')
        for (int j = 1; j <= n; j++)
            rfw[i][j] == INF ? out<<0<<' ' : out<<rfw[i][j]<<' ';
}

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

    read();
    solve();

    return 0;
}