Pagini recente » Cod sursa (job #1821301) | Cod sursa (job #3201536) | Cod sursa (job #178470) | Cod sursa (job #2218921) | Cod sursa (job #2812967)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
# define INF 0x3f3f3f3f
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
class Graph
{
private:
//number of nodes
int n;
//number of edges
int e;
//bool if graph is oriented
bool oriented;
//adj list for graph representation
vector<vector<int>> adj_list;
public:
Graph(int n, bool oriented)
{
this->n = n;
this->e = 0;
this->oriented = oriented;
//populate adj list with empty vectors
vector<int> empty;
for (int i = 0; i < n; i++)
{
this->adj_list.push_back(empty);
}
}
virtual ~Graph() {}
void royfloyd()
{
vector<vector<int>> distanceMatrix(101);
for (int i = 0; i < this->n; i++)
{
for (int j = 0; j < this->n; j++)
{
int distance;
fin >> distance;
if (i != j && distance == 0)
{
distance = INF;
}
distanceMatrix[i].push_back(distance);
}
}
for (int k = 0; k < this->n; k++)
{
for (int i = 0; i < this->n; i++)
{
for (int j = 0; j < this->n; j++)
{
distanceMatrix[i][j] = min(distanceMatrix[i][j], distanceMatrix[i][k] + distanceMatrix[k][j]);
}
}
}
for (int i = 0; i < this->n; i++)
{
for (int j = 0; j < this->n; j++)
{
if (distanceMatrix[i][j] == INF)
{
distanceMatrix[i][j] = 0;
}
fout << distanceMatrix[i][j] << " ";
}
fout << "\n";
}
}
};
int main()
{
int N;
fin >> N;
Graph g(N, true);
g.royfloyd();
return 0;
}