Cod sursa(job #2576033)

Utilizator MoodyFaresFares Mohamad MoodyFares Data 6 martie 2020 16:52:01
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#include <cstdio>
#include <algorithm>

using namespace std;

const int MAX_N = 100;
const int INF = 1000000;

int cost[5 + MAX_N][5 + MAX_N];

int main() {
  freopen("royfloyd.in", "r", stdin);
  freopen("royfloyd.out", "w", stdout);

  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%d", &cost[i][j]);
      if (cost[i][j] == 0)
        cost[i][j] = INF + 1;
    }
  }

  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (k != i && k != j && i != j)
          cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (cost[i][j] == INF + 1)
        cost[i][j] = 0;
      printf("%d ", cost[i][j]);
    }
    printf("\n");
  }

  return 0;
}