Pagini recente » Cod sursa (job #843745) | Cod sursa (job #1889732) | Cod sursa (job #50925) | Cod sursa (job #2208946) | Cod sursa (job #1705565)
#include<iostream>
#include<fstream>
#include <climits>
#define INF 696969
using namespace std;
int prec[100][100];
int cost[100][100];
bool path[100][100];
void drum(int x,int y)
{
if(x!=y)
drum(x,prec[x][y]);
cout<<y<<" ";
}
void roy_floyd(int n){
int i,j,k;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i==j || cost[i][j]==INF)
prec[i][j]=0;
else
prec[i][j]=i;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]>cost[i][k]+cost[k][j])
{
cost[i][j]=cost[i][k]+cost[k][j];
prec[i][j]=prec[k][j];
}
}
void print(int n){
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i!=j && cost[i][j]<INF)
{
cout<<"Costul de la "<<i<<" la "<<j<<" este "<<cost[i][j]<<" cu drumul: ";
drum(i,j);
cout<<endl;
}
}
int main(void)
{
int i,j,m,n,x,y,k;
fstream f,out;
f.open("royfloyd.in",ios::in);
out.open("royfloyd.out",ios::out);
f>>n;
//cout<<n<<" "<<m<<endl;
/*for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i!=j)
cost[i][j]=INF;*/
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
f>>cost[i][j];
if (i != j && cost[i][j] == 0)
cost[i][j] = INF;
}
roy_floyd(n);
//calea efectiva
//print(n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
if (cost[i][j] < INF)
out << cost[i][j] << " ";
else
out << "0 ";
out<<endl;
}
}