Cod sursa(job #3321045)

Utilizator n6v26rDedu Razvan Matei n6v26r Data 8 noiembrie 2025 00:47:46
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
// SOURCE: infoarena.ro
#include <climits>
#include <stdio.h>

#define MAXN 300
#define MIN(a, b) ((a) < (b) ? (a) : (b))

struct Metrics {
  int dist;
  int strazi;
  bool operator<(const Metrics &other) {
    if (this->dist != other.dist)
      return this->dist < other.dist;
    return this->strazi > other.strazi;
  }
  friend Metrics operator+(const Metrics &a, const Metrics &b) {
    return {a.dist + b.dist, a.strazi + b.strazi};
  }
};

int n;
Metrics dp[MAXN][MAXN];

int main() {
  for (int i = 0; i < MAXN; i++)
    for (int j = 0; j < MAXN; j++)
      dp[i][j] = {INT_MAX, 0};

  freopen("royfloyd.in", "r", stdin);
  freopen("royfloyd.out", "w", stdout);
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%d", &dp[i][j].dist);
      dp[i][j].strazi = (i != j);
    }
  }

  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      if (dp[i][k].dist == INT_MAX)
        continue;
      for (int j = 0; j < n; j++) {
        if (dp[k][j].dist == INT_MAX)
          continue;

        dp[i][j] = MIN(dp[i][j], dp[i][k] + dp[k][j]);
      }
    }
  }

  for(int i=0; i<n; i++){
    for(int j=0; j<n; j++)
      printf("%d ", dp[i][j].dist);
    printf("\n");
  }
  return 0;
}