Cod sursa(job #1974615)

Utilizator Mihai_PredaPreda Mihai Dragos Mihai_Preda Data 28 aprilie 2017 11:01:07
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 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[2][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()
{
    int current = 0, last = 1;
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            dp[current][i][j] = cost[i][j];

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

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

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