Pagini recente » Cod sursa (job #2750363) | Cod sursa (job #2626951) | Cod sursa (job #2626082) | Cod sursa (job #1968584) | Cod sursa (job #1205699)
///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(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;
}