Cod sursa(job #2469459)

Utilizator ValentinStStamate Valentin ValentinSt Data 7 octombrie 2019 12:59:43
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <fstream>
#include <iostream>
using namespace std;

#define NMAX 100
#define MAXDIST 1001

ifstream in("royfloyd.in");
ofstream out("royfloyd.out");

int a[NMAX][NMAX], n, dist[NMAX][NMAX];

void getArray();
void floydWarshall();

int main(){
  in >> n;
  getArray();
  floydWarshall();
  
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      out<<dist[i][j]<<" ";
    }
    out<<"\n";
  }

  return 0;
}

void getArray(){
  for (int i = 0; i < n; i++){
    for (int j = 0; j < n; j++){
      in>>a[i][j];
      dist[i][j] = a[i][j];
    }
  }
}

void floydWarshall(){
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      if(dist[i][j] == 0 && i != j){
        dist[i][j] = MAXDIST;
      }
    }
  }

  for(int k = 0; k < n; k++){
    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++){
        int temp = dist[i][k] + dist[k][j];
        if(dist[i][j] > temp){
          dist[i][j] = temp;
        }
      }
    }
  }

}