Cod sursa(job #1970186)

Utilizator Dddarius95Darius-Florentin Neatu Dddarius95 Data 18 aprilie 2017 23:50:14
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.39 kb
/*
    skel_graph - RoyFloyd - O(n^3)
    (Sau cu notatia din lab O(V^3))
    http://www.infoarena.ro/problema/royfloyd
*/

#include <iostream>
#include <cstdio>
#include <fstream>
#include <vector>
#include <bitset>
#include <cassert>
#include <cstring>
#include <algorithm>
#define NMAX 109  // ATENTIE la cat e in problema curenta !!!
#define oo (1 << 20) // ATENTIE la cat e in problema curenta !!!
using namespace std;

// aceleasi semnificatii ca la BFS
int n;
int d[NMAX][NMAX], last[NMAX][NMAX];
// last[i][j] = k, daca drumul min de la i la j are forma
// i->...->k->j (k este ultimul nod prin care trec inainte de a ajunge la destinatie)

void read_input() {
    cin >> n;

    for (int x = 1; x <= n; ++x) {
        for (int y = 1; y <= n; ++y) {
            int c; // x -> y, de cost c
            cin >> c;

            if (c == 0) {
                c = oo;
            }

            d[x][y] = c;
            last[x][y] = x;
        }
    }
}

void RoyFloyd() {
    for (int k = 1; k <= n; ++k) {
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                if (i != j && d[i][k] + d[k][j] < d[i][j]) {
                    d[i][j] = d[i][k] + d[k][j];
                    last[i][j] = last[k][j];
                }
            }
        }
    }
}

// in solve scriu tot ce tine de rezolvare - e ca un main
void solve() {
   RoyFloyd();
}

// rebuild source -> ... -> node (if exists)
void rebuild_path_recursive(int node, int source) {
    if (d[ node ][source] == oo) {
        cout << "Drum inexistent!";
        return;
    }

    if (node == source) {
        cout << node << " ";
        return;
    }

    // rebuild first source -> ...-> p[node]
    rebuild_path_recursive(last[node][source], source);

    // then p[node] -> node
    cout << node << " ";
}


// afisez vectori si tot ce mai trebuie
void print_output() {
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            if (d[i][j] == oo) {
                d[i][j] = 0;
            }
            cout << d[i][j] << " ";
        }
        cout << "\n";
    }
}

// puteti pastra main-ul mereu cum e acum
int main() {
    // las linia urmatoare daca citesc din fisier
    assert( freopen("royfloyd.in", "r", stdin) != NULL);

    // las linia urmatoare daca afisez in fisier
    assert( freopen("royfloyd.out", "w", stdout) != NULL) ;

    read_input();
    solve();
    print_output();

    return 0;
}