Cod sursa(job #2691408)

Utilizator denisa.iordacheIordache Denisa-Elena denisa.iordache Data 28 decembrie 2020 15:31:21
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

const int NMAX = 110;
const int INF = 1e9;
int N;
int cost[NMAX][NMAX], lant[NMAX][NMAX]; //lant[i][j] distanta minima a lantului dintre i si j



int main() {

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

    f>>N;

    for(int i = 1; i <=N; i++)
    {
        for(int j = 1; j<=N; j++)
        {
            f>>cost[i][j];
            if(i != j && cost[i][j] == 0)
                cost[i][j] = INF;
        }
    }



    for(int k = 1; k <= N; k++)
        for(int i = 1; i <= N; i++)
            for(int j = 1; j<= N; j++)
                if (cost[i][j] > cost[i][k] + cost[k][j])
                {
                    cost[i][j] = cost[i][k] + cost[k][j];
                }


    for(int i = 1 ; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
            if(cost[i][j] == INF)
                g<<0<<" ";
            else
            g<<cost[i][j]<<" ";
       g<<endl;
    }

    return 0;
}