Cod sursa(job #1877193)

Utilizator rares00Foica Rares rares00 Data 13 februarie 2017 08:58:10
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <iostream>
#include <fstream>
#define INF 1001
using namespace std;
ifstream in("royfloyd.in");
ofstream out("royfloyd.out");

int n;
int cost[102][102];

void royfloyd()
{
    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]) //daca avem cost optim de la i la j prin k
                    cost[i][j] = cost[i][k]+cost[k][j]; //actalizeaza
}

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

    royfloyd();

    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=n;++j)
        {
            if(cost[i][j]==INF)
                out<<"0 ";
            else
                out<<cost[i][j]<<" ";
        }
        out<<"\n";
    }

    return 0;
}