Cod sursa(job #941146)

Utilizator andrei0610Andrei Constantinescu andrei0610 Data 18 aprilie 2013 01:38:18
Problema Floyd-Warshall/Roy-Floyd Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
//
//  main.cpp
//  Algoritmul lui Roy-Floyd
//
//  Created by Andrei Constantinescu on 18.04.2013.
//  Copyright (c) 2013 Andrei Constantinescu. All rights reserved.
//

#include <fstream>

#define MAX 5000
using namespace std;

int n, a[100][100];

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

void initializare() {
    int i, j;
    f >> n;
    for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++)
            if (i != j)
                a[i][j] = MAX;
}

void citire() {
    int i, j, cost = 0;
    for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++) {
            f >> cost;
            a[i][j] = cost;
        }
    f.close();
}

void royfloyd() {
    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j =1; j <= n; j++)
                if (a[i][k] + a[k][j] < a[i][j])
                    a[i][j] = a[i][k] + a[k][j];
}

void afisare() {
    for (int i = 1; i <= n; i++) {
        for (int j =1; j <= n; j++)
            if (a[i][j] < MAX)
                g << a[i][j] << " ";
        g<< endl;
    }
}

int main() {
    initializare();
    citire();
    royfloyd();
    afisare();
    g.close();
}