Cod sursa(job #3211493)

Utilizator andrei_botorogeanuBotorogeanu Andrei andrei_botorogeanu Data 9 martie 2024 13:44:03
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include<iostream>
#define fin "royfloyd.in"
#define fout "royfloyd.out"

#define size 1001
const float inf = 1.e21;
int nodes;
float mat[size][size];
int i, j;
float c;

void loadGraph() {
  freopen(fin, "r", stdin);
  scanf("%d", &nodes);
  for(int i=1; i<=nodes; i++)
    for(int j=1; j<=nodes; j++)
      std :: cin >> mat[i][j];
}

void showMatrix_costs() {

  freopen(fout, "w", stdout);
  for(int i=1; i<=nodes; i++) {
    for(int j=1; j<=nodes; j++)
      std :: cout<<mat[i][j]<<" ";
      //printf("%.1f ", mat[i][j]);
    printf("\n");
  }
}
void Roy_Floyd(/* arguments */) {
  /* code */
  for(int k=1; k<=nodes; k++)
    for(int i=1; i<=nodes; i++)
      for(int j=1; j<=nodes; j++)
      {
        if(mat[i][j] > mat[i][k] + mat[k][j])
          mat[i][j] = mat[i][k] + mat[k][j];
      }
}
/*
Se cauta drumurile optime intre oricare doua noduri unde drumurile pot trece
numai prin nodul intermediar 1(k = 1), dupa aceea se cauta drumurile optime intre oricare doua
noduri unde drumurile pot trece numai prin nodul intermediar 2, si tot asa(3, 4, 5,...),
iar la fiecare pas se optimizeaza(daca este posibil ),
*/
int main()
{
  loadGraph();

  Roy_Floyd();

  showMatrix_costs();
}