Pagini recente » Cod sursa (job #1789328) | Cod sursa (job #2815022)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const int inf = 2000000000;
class weightedGraph{
private:
int m_numberOfNodes;
int m_numberOfEdges;
vector < vector<int> > m_costMatrix;
public:
weightedGraph(int numberOfNodes, int numberOfEdges, vector <vector<int>> &costMatrix){
m_numberOfNodes = numberOfNodes;
m_numberOfEdges = numberOfEdges;
if(costMatrix.size() != numberOfNodes + 1){
costMatrix.resize(numberOfNodes + 1, vector <int> (numberOfNodes + 1));
}
m_costMatrix = costMatrix;
//costMatrix.clear();
}
virtual ~weightedGraph() {
m_costMatrix.clear();
}
int getNumberOfNodes() const {
return m_numberOfNodes;
}
void setNumberOfNodes(int mNumberOfNodes) {
m_numberOfNodes = mNumberOfNodes;
}
int getNumberOfEdges() const {
return m_numberOfEdges;
}
void setNumberOfEdges(int mNumberOfEdges) {
m_numberOfEdges = mNumberOfEdges;
}
const vector<vector<int>> &getCostMatrix() const {
return m_costMatrix;
}
void setCostMatrix(const vector<vector<int>> &mCostMatrix) {
m_costMatrix = mCostMatrix;
}
void setEdgeCost(int i, int j, int value){
if(i <= m_costMatrix.size()){
if(j <= m_costMatrix[i].size()){
m_costMatrix[i][j] = value;
}
}
}
void royFloydWarshall(){
for(int k = 1; k <= m_numberOfNodes; ++k){
for(int i = 1; i <= m_numberOfNodes; ++i){
for(int j = 1; j <= m_numberOfNodes; ++j){
m_costMatrix[i][j] = min(m_costMatrix[i][j], m_costMatrix[i][k] + m_costMatrix[k][j]);
}
}
}
}
};
int main() {
ifstream f("royfloyd.in");
ofstream g("royfloyd.out");
int n, m = 0;
f >> n;
vector <vector<int>> v;
v.resize(n + 1, vector <int> (n + 1, 0));
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
f >> v[i][j];
++m;
if(i != j && v[i][j] == 0){
v[i][j] = inf;
}
}
}
weightedGraph graf(n, m, v);
graf.royFloydWarshall();
v = graf.getCostMatrix();
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
if(v[i][j] != inf){
g << v[i][j] << " ";
}
else{
g << 0 << " ";
}
}
g << "\n";
}
return 0;
}