Cod sursa(job #2489533)

Utilizator xCata02Catalin Brita xCata02 Data 8 noiembrie 2019 21:22:20
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <bits/stdc++.h>
using namespace std;

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

#define cin fin
#define cout fout

int n, m, x, y, cost;
int a[101][101];

void read_graph()
{
    cin >> n >> m;
    for(int i=1; i<=m; i++)
    {
        cin >> x >> y >> cost;
        a[x][y] = cost;
    }
}

void read_mat()
{
    cin >> n;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            cin >> a[i][j];
        }
    }
}

void solve()
{
    for(int k=1; k<=n; k++)
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(((a[i][j] > a[i][k] + a[k][j]) || !a[i][j]) && i != j && a[i][k] && a[k][j])
                {
                    a[i][j] = a[i][k] + a[k][j];
                }
            }
        }
    }
}

void print()
{
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
}

int main()
{
    read_mat();
    solve();
    print();
}