Cod sursa(job #2611504)

Utilizator diana.megeleaDiana Megelea diana.megelea Data 6 mai 2020 23:17:07
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp-32 Status done
Runda Arhiva educationala Marime 1.57 kb
#include <bits/stdc++.h>
using namespace std;
 
#define NMAX 110
#define oo (1 << 20)
 
class Task {
 public:
    void solve() {
        read_input();
        get_result();
        print_output();
    }
 
 private:
    int n;
    int dist[NMAX][NMAX];
 
    void read_input() {
        ifstream in("royfloyd.in");
        in >> n;

        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                int cost;
                in >> cost;
                
                if (cost == 0) {
                    cost = oo;
                }

                dist[i][j] = cost;
            }
        }

        in.close();
    }

    void get_result() {
        Roy_Floyd();
    }
 
    void Roy_Floyd() {
        for (int k = 1; k <= n; ++k) {
            for (int i = 1; i <= n; ++i) {
                for (int j = 1; j <= n; ++j) {
                    if (i != j && dist[i][k] + dist[k][j] < dist[i][j]) {
                        dist[i][j] = dist[i][k] + dist[k][j];
                    }
                }
            }
        }
    }
 
    void print_output() {
        ofstream out("royfloyd.out");

        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                if (dist[i][j] == oo) {
                    dist[i][j] = 0;
                }

                out << dist[i][j] << " ";
            }    
        }
        out << "\n";

        out.close();
    }
};
 

int main() {
    Task *task = new Task();
    task->solve();
    delete task;
    return 0;
}