Cod sursa(job #1768183)

Utilizator danielNiculaeDaniel Niculae danielNiculae Data 30 septembrie 2016 13:53:06
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.58 kb
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   royfloyd.cpp
 * Author: octavian
 *
 * Created on 30 September 2016, 11:31
 */

#include <cstdlib>
#include <algorithm>

using namespace std;


int N;
const int NMAX = 105;
const int INF = 1000000005;

int dist[NMAX][NMAX];


/*
 * 
 */
int main(int argc, char** argv) {

    FILE *fin = fopen("royfloyd.in", "r");
    FILE *fout = fopen("royfloyd.out", "w");


    fscanf(fin, "%d", &N);
    
    for(int i = 1 ; i <= N ; i++) {
        for(int j = 1 ; j <= N ; j++) {
            fscanf(fin, "%d", &dist[i][j]);
            if(dist[i][j] == 0) {
                dist[i][j] = INF;
            }
        }
    }
    
//    for(int i = 1 ; i <= N ; i++) {
//        for(int j = 1 ; j <= N ; j++) {
//            fprintf(fout, "%d ", dist[i][j]);
//        }
//        fprintf(fout, "\n");
//    }
//    
    for(int k = 1 ; k <= N ; k++) {
        for(int i = 1 ; i <= N ; i++) {
            for(int j = 1 ; j <= N ; j++) {
                if(i != j) {
                    dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
                }
            }
        }
    }
    
    for(int i = 1 ; i <= N ; i++) {
        for(int j = 1 ; j <= N ; j++) {
            if(dist[i][j] == INF) {
               fprintf(fout, "0 ");
            } else {
               fprintf(fout, "%d ", dist[i][j]);
            }
        }
        fprintf(fout, "\n");
    }
    
    fclose(fin);
    fclose(fout);
    
    return 0;
}