Pagini recente » Cod sursa (job #691823) | Cod sursa (job #1807548) | Cod sursa (job #447148) | Cod sursa (job #255675) | Cod sursa (job #775616)
Cod sursa(job #775616)
#include <fstream>
#include <vector>
#include <limits>
typedef std::vector<std::vector<signed int> > matrix;
void FloydWarshall (matrix &graph_matrix, const signed int LONGEST_POSSIBLE_PATH)
{
signed int path, new_path;
unsigned short intermediate, from, to;
const unsigned short SIZE(graph_matrix.size());
for (intermediate = 0 ; intermediate < SIZE ; ++intermediate)
for (from = 0 ; from < SIZE ; ++from)
{
path = graph_matrix[from][intermediate];
if (path == LONGEST_POSSIBLE_PATH)
continue;
for (to = 0 ; to < SIZE ; ++to)
{
new_path = graph_matrix[intermediate][to];
if (new_path == LONGEST_POSSIBLE_PATH || from == to)
continue;
new_path += path;
if (new_path < graph_matrix[from][to])
graph_matrix[from][to] = new_path;
}
}
}
int main (void)
{
unsigned short n;
std::ifstream input("royfloyd.in");
input >> n;
matrix graph_matrix(n,std::vector<signed int>(n));
matrix::iterator row(graph_matrix.begin());
matrix::const_iterator ROW_END(graph_matrix.end());
std::vector<signed int>::iterator collum;
std::vector<signed int>::const_iterator COLLUM_END;
const signed int LONGEST_POSSIBLE_PATH(std::numeric_limits<signed int>::max());
do
{
for (collum = row->begin(), COLLUM_END = row->end() ; collum != COLLUM_END ; ++collum)
{
input >> *collum;
if (!*collum)
*collum = LONGEST_POSSIBLE_PATH;
}
++row;
}
while (row != ROW_END);
input.close();
FloydWarshall(graph_matrix,LONGEST_POSSIBLE_PATH);
std::ofstream output("royfloyd.out");
row = graph_matrix.begin();
ROW_END = graph_matrix.end();
do
{
collum = row->begin();
COLLUM_END = row->end();
while (true)
{
if (!*collum || *collum == LONGEST_POSSIBLE_PATH)
output.put('0');
else
output << *collum;
++collum;
if (collum == COLLUM_END)
break;
output.put(' ');
}
output.put('\n');
++row;
}
while (row != ROW_END);
output.close();
return 0;
}