Cod sursa(job #2673225)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 16 noiembrie 2020 12:02:14
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.77 kb
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cmath>	
#define debug(x) cerr << #x << " " << x << "\n"
#define debugsp(x) cerr << #x << " " << x << " "

using namespace std;

const int INF = 2e9;
const int N = 100;

int dp[5 + N][5 + N];
int n;

void Roy_Floyd() {
  for(int k = 1; k <= n; k++) {
    for(int i = 1; i <= n; i++) {
      for(int j = 1; j <= n; j++) {
        if(i != j) { 
          if(dp[i][k] != 0 && dp[k][j] != 0 && (dp[i][k] + dp[k][j] < dp[i][j] || dp[i][j] == 0))
            dp[i][j] = dp[i][k] + dp[k][j];
        }
      }
    }
  }
}

int main() {
  freopen("royfloyd.in", "r", stdin);
  freopen("royfloyd.out", "w", stdout);
  scanf("%d", &n);

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

  Roy_Floyd();

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

  fclose(stdin);
  fclose(stdout);
  return 0;
}