Cod sursa(job #3256247)

Utilizator Tudor.1234Holota Tudor Matei Tudor.1234 Data 13 noiembrie 2024 21:27:38
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.46 kb
#include "bits/stdc++.h"
#define inf  INT_MAX
#define int long long int
const int SIZE = 100;
int dist[SIZE + 5][SIZE + 5];
int n;
void Debug(){
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			if(dist[i][j] == inf)
				std :: cout << 0 << ' ';
			else
			 std :: cout << dist[i][j] << ' ';
		}
		std :: cout << '\n';
	}
}
void Floyd() {
    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (dist[i][k] != inf && dist[k][j] != inf && dist[i][j] > dist[i][k] + dist[k][j]) {
                    dist[i][j] = dist[i][k] + dist[k][j];
                }
            }
        }
    }
}

void Solve() {
    std::cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            std::cin >> dist[i][j];
            if (dist[i][j] == 0 && i != j) { 
                dist[i][j] = inf;
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        dist[i][i] = 0;
    }

    Floyd();

    // Afișare rezultat
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (dist[i][j] == inf)
                std::cout << 0 << ' ';
            else
                std::cout << dist[i][j] << ' ';
        }
        std::cout << '\n';
    }
}

signed main(){
	freopen("royfloyd.in", "r", stdin);
	freopen("royfloyd.out", "w", stdout);
	std :: ios_base :: sync_with_stdio(false);
	std :: cin.tie(0);
	std :: cout.tie(0);
	Solve();
	return 0;
}