Cod sursa(job #1200886)

Utilizator bvanceaBogdan Vancea bvancea Data 23 iunie 2014 19:33:52
Problema Floyd-Warshall/Roy-Floyd Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include <fstream>

const int INFINITY = 1001;

using namespace std;

void floyd_warshall() {

}

int main(void) {
    ifstream in("royfloyd.in");
    ofstream out("royfloyd.out");
    int t;
    in >> t;
    int w[100][100];    
    for (int i = 0; i < t; i++) {
        for (int j = 0; j < t; j++) {
            in >> w[i][j]; 
            if (w[i][j] == 0 && i != j) {
                w[i][j] = INFINITY;
            } 
        }
    }

    //run floyd warshall:
    //  for any 2 nodes i and j, if the distance going from i to j through
    //  another node k is smaller than dist(i,j), update dist[i,j]
    for (int i = 0; i < t; i++) {
        for (int j = 0; j < t; j++) {
            for (int k = 0; k < t; k++) {
                if (w[i][j] > w[i][k] + w[k][j]) {
                    w[i][j] = w[i][k] + w[k][j];
                }
            }
        }
    }

    //output distance matrix
    for (int i = 0; i < t; i++) {
        for (int j = 0; j < t; j++) {
            if (w[i][j] == INFINITY) {
                w[i][j] = 0;
            }
            out << w[i][j] << " "; 
        }
        out << "\n";
    }
    return 0;
}