Cod sursa(job #1822677)

Utilizator AetheryonStefan Bereghici Aetheryon Data 5 decembrie 2016 12:56:33
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.62 kb
#include <bits/stdc++.h>
using namespace std;
const int Nmax = 100;
const int inf = 0x3f3f3f;

class DirectedGraph {
    private :
        int nodes;
        int w[Nmax][Nmax];
    public :
        DirectedGraph(int n){
            this->nodes = n;
        }

        void setWeight(int from,int to,int weight){
            if (weight==0) {
                w[from][to] = inf;
            }
            else {
                w[from][to] = weight;
            }
        }

        void royFloyd(){
            for (int k=0;k<nodes;++k){
                for (int j=0;j<nodes;++j){
                    for (int i=0;i<nodes;++i){
                        if (i!=j) {
                            w[i][j] = min(w[i][j],w[i][k] + w[k][j]);
                        }
                    }
                }
            }
        }

        int getWeight(int from,int to){
            return w[from][to];
        }
};

int main(void){
    FILE *in = fopen("royfloyd.in","r");
    FILE *out = fopen("royfloyd.out","w");
    int n,weight;
    fscanf(in,"%d",&n);
    DirectedGraph graph = DirectedGraph(n);
    for (int i=0;i<n;++i){
        for (int j=0;j<n;++j){
            fscanf(in,"%d",&weight);
            graph.setWeight(i,j,weight) ;
        }
    }
    graph.royFloyd();
    for (int i=0;i<n;++i){
        for (int j=0;j<n;++j){
            weight = graph.getWeight(i,j);
            if (weight == inf) {
                fprintf(out,"0 ");
            }
            else {
                fprintf(out,"%d ",weight);
            }
        }
        fprintf(out,"\n");
    }
    return 0;
}