Pagini recente » Cod sursa (job #3278491) | Cod sursa (job #1000864) | Cod sursa (job #1887088) | Cod sursa (job #2730204) | Cod sursa (job #443122)
Cod sursa(job #443122)
#include <iostream>
#include <fstream>
using namespace std;
typedef unsigned long ulong;
int main(int argc, char * argv[])
{
const char * inFile = "royfloyd.in";
const char * outFile = "royfloyd.out";
ifstream fin(inFile);
ofstream fout(outFile);
#ifndef NDEBUG
if(!fin || !fout)
{
cerr << "Error opening one of \"" << inFile << "\" or \"" << outFile << "\"" << endl;
return -1;
}
#endif
/**
* Read the data in.
*/
ulong numVertices;
fin >> numVertices;
ulong costMatrix[numVertices + 1][numVertices + 1];
ulong x, y;
long cost;
for(ulong i = 1; i <= numVertices; i++)
{
for(ulong j = 1; j <= numVertices; j++)
{
fin >> costMatrix[i][j];
}
}
/**
* Solve the problem.
*/
for(ulong k = 1; k <= numVertices; k++)
{
for(ulong i = 1; i <= numVertices; i++)
{
for(ulong j = 1; j <= numVertices; j++)
{
if(i != j)
{
if(costMatrix[i][k] != 0 && costMatrix[k][j] != 0)
{
ulong through_k = costMatrix[i][k] + costMatrix[k][j];
if(through_k < costMatrix[i][j] || costMatrix[i][j] == 0)
costMatrix[i][j] = through_k;
}
}
}
}
}
for(ulong i = 1; i <= numVertices; i++)
{
for(ulong j = 1; j <= numVertices; j++)
{
fout << costMatrix[i][j] << " ";
}
fout << "\n";
}
/**
* Cleanup!
*/
fout.close();
fin.close();
return 0;
}