Cod sursa(job #1184494)

Utilizator lvamanuLoredana Vamanu lvamanu Data 12 mai 2014 20:58:41
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 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) {
                    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++) {
            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;
            if (x == 0 && i != j) {
                dist[i][j] = D;
            } else {
                dist[i][j] = x;
            }
        }
    }
    floydWarshall(N);
    print(N);

    fclose(stdout);
    return 0;
}