Cod sursa(job #1645701)

Utilizator VladTiberiuMihailescu Vlad Tiberiu VladTiberiu Data 10 martie 2016 13:21:04
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>

#define NMax 105
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
int a[NMax][NMax];

int main()
{
    freopen("royfloyd.in","r",stdin);
    freopen("royfloyd.out","w",stdout);
    scanf("%d",&n);
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            scanf("%d",&a[i][j]);
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            if(a[i][j] == 0 && i != j)
                a[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(a[i][j] > a[i][k] + a[k][j] && i != j){
                    a[i][j] = a[i][k] + a[k][j];
                }
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            if(a[i][j] == INF && i != j)
                printf("0 ");
            else
                printf("%d ",a[i][j]);
        }
        printf("\n");
    }
    return 0;
}