Cod sursa(job #1974614)

Utilizator Mihai_PredaPreda Mihai Dragos Mihai_Preda Data 28 aprilie 2017 10:59:30
Problema Floyd-Warshall/Roy-Floyd Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <iostream>
#include <fstream>

using namespace std;

const int nMax = 105;
const int INF = (1 << 26);

int n;
int cost[nMax][nMax];
int dp[nMax][nMax][nMax];

void citire()
{
    ifstream in("royfloyd.in");
    in >> n;
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
        {
            in >> cost[i][j];
            if(i != j && cost[i][j] == 0)
                cost[i][j] = INF;
        }
    in.close();
}

void rezolvare()
{
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            dp[0][i][j] = cost[i][j];

    for(int k = 1; k <= n; ++k)
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= n; ++j)
            {
                dp[k][i][j] = min(dp[k-1][i][j], dp[k-1][i][k] + dp[k-1][k][j]);
            }

    ofstream out("royfloyd.out");
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= n; ++j)
        {
            out << dp[n][i][j] << " ";
        }
        out << "\n";
    }
    out.close();
}

int main()
{
    citire();
    rezolvare();
    return 0;
}