Cod sursa(job #1912962)

Utilizator VladTiberiuMihailescu Vlad Tiberiu VladTiberiu Data 8 martie 2017 11:20:48
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <bits/stdc++.h>

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

const int NMax = 103;
const int INF = 0x3f3f3f3f;

int n,m,x;
int dp[NMax][NMax];

int main()
{
    f >> n;
    memset(dp,INF,sizeof(dp));
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            f >> x;
            if(x == 0)
                dp[i][j] = INF;
            else
                dp[i][j] = x;
        }
    }
    for(int inter = 1; inter <= n; ++inter){
        for(int i = 1; i <= n; ++i){
            for(int j = 1; j <= n; ++j){
                if(i != j && dp[i][j] > dp[i][inter] + dp[inter][j]){
                    dp[i][j] = dp[i][inter] + dp[inter][j];
                }
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            if(dp[i][j] == INF){
                g << 0 << ' ';
            }else
                g << dp[i][j] << ' ';
        }
        g << '\n';
    }
    return 0;
}