Pagini recente » Cod sursa (job #2572898) | Cod sursa (job #3169600) | Cod sursa (job #1571763) | Cod sursa (job #1682958) | Cod sursa (job #1454354)
#include <fstream>
#include <vector>
#include <limits>
using namespace std;
constexpr int inf = numeric_limits<int>::max();
int main(){
ifstream f("royfloyd.in");
ofstream g("royfloyd.out");
int n;
f >> n;
vector<vector<int> > dist(n, vector<int>(n));
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
f >> dist[i][j];
if(dist[i][j] == 0){
dist[i][j] = inf; } } }
for(int k = 0; k < n; ++k){
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
if(i!=j && dist[i][k] != inf && dist[k][j] != inf){
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } }
for(const auto& r : dist){
for(const auto y : r){
g << (y==inf ? 0 : y) << ' '; }
g << '\n'; }
return 0; }