Cod sursa(job #1184461)

Utilizator lvamanuLoredana Vamanu lvamanu Data 12 mai 2014 19:47:40
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 kb
#include <iostream>
#include <stdlib.h>
#include <stdio.h>

using namespace std;
#define D 1005

int dist[100][100];

void floydWarshall(int N) {
    for (int k = 0; k < N; k++) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if ((dist[i][k] != 0 && dist[k][j] != 0) || (i == k || k == j)) {
                    int temp = dist[i][k] + dist[k][j];
                    if (dist[i][j] > temp) {
                        dist[i][j] = temp;
                    }
                }
            }
        }
    }
}

void print(int N) {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (dist[i][j] == D) {
                cout << 0;
            } else {
                cout << dist[i][j];
            }
            if (j < N - 1) {
                cout << " ";
            }
        }
        cout << endl;
    }
}

int main() {
    freopen("royfloyd.in", "r", stdin);
    freopen("royfloyd.out", "w", stdout);
    int N;
    cin >> N;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            int x;
            cin >> x;
            dist[i][j] = x;

        }
    }
    floydWarshall(N);
    print(N);

    fclose(stdout);
    return 0;
}