Pagini recente » Cod sursa (job #2070015) | Cod sursa (job #2050213) | Cod sursa (job #1482911) | Cod sursa (job #1895597) | Cod sursa (job #2685070)
// Floyd-Warshall, Roy-Floyd.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
#define MAX 105
const int INF = 0x3F3F3F3F;
std::ifstream fin("royfloyd.in");
std::ofstream fout("royfloyd.out");
using namespace std;
// Algoritmul Roy-Warshall-Floyd pe grafuri orientate cu costuri
int N, M;
int D[MAX][MAX];
void Read_Graph()
{
fin >> N;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++) {
fin >> D[i][j];
if (D[i][j] == 0 && i != j)
D[i][j] = INF;
}
}
void Roy_Floyd()
{
int k, i, j;
for (k = 1; k <= N; k++)
for (i = 1; i <= N; i++)
for (j = 1; j <= N; j++)
if (D[i][j] > D[i][k] + D[k][j])
D[i][j] = D[i][k] + D[k][j];
}
void Write_Cost()
{
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++)
if (D[i][j] == INF)
fout << "0 ";
else
fout << D[i][j] << ' ';
fout << '\n';
}
}
int main()
{
Read_Graph();
Roy_Floyd();
Write_Cost();
return 0;
}