Pagini recente » Cod sursa (job #405217) | Cod sursa (job #2621021) | Cod sursa (job #3042381) | Cod sursa (job #2749123) | Cod sursa (job #875461)
Cod sursa(job #875461)
#include <iomanip>
#include <fstream>
#include <climits>
using namespace std;
const int Max = 101;
int i, j, k, length, value, local_aux, Cost[Max][Max]/*, Path[Max][Max]*/;
void initialiseData(){
for (i = 1; i <= length; i++){
for (j = 1; j < length; j++){
Cost[i][j] = INT_MAX;
//Path[i][j] = 0;
}
Cost[i][i] = 0;
//Path[i][i] = 0;
}
}
void readData(ifstream& in){
in >> length;
initialiseData();
/*while (in && !in.eof() && in >> i >> j >> value){
Cost[i][j] = value;*/
for (i = 1; i <= length; i++)
for (j = 1; j <= length; j++)
in >> Cost[i][j];
}
void process(){
for (k = 1; k <= length; k++){
for (i = 1; i <= length; i++){
for (j = 1; j <= length; j++){
if (Cost[i][k] < INT_MAX && Cost[k][j] < INT_MAX){
local_aux = Cost[i][k] + Cost[k][j];
if (local_aux < Cost[i][j]){
Cost[i][j] = local_aux;
//Path[i][j] = k;
}
}
}
}
}
}
//void createPath(int x, int y, ofstream& out){
// k = Path[x][y];
// if (k){
// createPath(x,k,out);
// createPath(k,y,out);
// }
// else{
// out << " " << y;
// }
//}
void writeData(ofstream& out){
//out << "Cost: " << endl;
for (i = 1; i <= length; i++){
for (j = 1; j <= length; j++){
if (Cost[i][j] < INT_MAX){
out << /*setw(5) <<*/ Cost[i][j] << " ";
}
else{
out << /*setw(5) << "oo"*/ "0 ";
}
}
out << endl;
}
/*out << endl << endl << "Path: " << endl;
for (i = 1; i <= length; i++){
for (j = 1; j <= length; j++){
if (i != j && Cost[i][j] < INT_MAX){
out << i;
createPath(i,j,out);
out << endl;
}
}
out << endl;
}*/
}
int main(int argc, char *argv[]){
ifstream in("roy.in");
ofstream out("roy.out");
readData(in);
process();
writeData(out);
return 0;
}