Pagini recente » Cod sursa (job #2058499) | Cod sursa (job #2108347) | Cod sursa (job #3041598) | Cod sursa (job #2391333) | Cod sursa (job #1206010)
///ROY-FLOYD
#include <fstream>
#include <vector>
#include <limits>
#include <iostream>
using namespace std;
typedef numeric_limits<int> I_LIM;
void royFloyd(vector<vector<int> >& neighMat) {
unsigned numNodes = neighMat.size();
for(unsigned k=0; k < numNodes; k++)
for(unsigned i=0; i < numNodes; i++)
for(unsigned j=0; j < numNodes; j++)
if(i != j && neighMat[i][k] != I_LIM::max() && neighMat[k][j] != I_LIM::max() &&
neighMat[i][k] + neighMat[k][j] < neighMat[i][j])
neighMat[i][j] = neighMat[i][k] + neighMat[k][j];
}
int main() {
ifstream inStr("royfloyd.in");
ofstream outStr("royfloyd.out");
unsigned numNodes;
inStr >> numNodes;
vector<vector<int> > neighMat(numNodes, vector<int>(numNodes));
for(unsigned i=0; i < numNodes; i++)
for(unsigned j=0; j < numNodes; j++) {
inStr >> neighMat[i][j];
if(i != j && neighMat[i][j] == 0)
neighMat[i][j] = I_LIM::max();
}
royFloyd(neighMat);
for(unsigned i=0; i < numNodes; i++) {
for(unsigned j=0; j < numNodes; j++)
if(neighMat[i][j] == I_LIM::max())
outStr << 0 << ' ';
else
outStr << neighMat[i][j] << ' ';
outStr << '\n';
}
return 0;
}