Cod sursa(job #3320794)

Utilizator stefanchpStefan Chiper stefanchp Data 7 noiembrie 2025 14:08:36
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
#include <iostream>
#include <fstream>
#define infinit 1e9
using namespace std;

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

int n, m;
int a[105][105], c[105][105];

void citire()
{
    int x, y, cost;
    fin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            if (i != j)
                c[i][j] = infinit;
    for (int i = 1; i <= m; i++)
    {
        fin >> x >> y >> cost;
        a[x][y] = 1;
        c[x][y] = cost;
    }
}

void RoyFloyd()
{
    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                c[i][j] = c[i][k] + c[k][j] < c[i][j] ? c[i][k] + c[k][j] : c[i][j];
}

int main()
{
    citire();
    RoyFloyd();
    for (int i = 1; i <= n; i++, fout << '\n')
        for (int j = 1; j <= n; j++)
            if (c[i][j] != infinit)
                fout << c[i][j] << ' ';
            else fout << 0 << ' ';
    return 0;
}