Pagini recente » Cod sursa (job #1427131) | Cod sursa (job #1422527) | Cod sursa (job #383593) | Cod sursa (job #1048274) | Cod sursa (job #884527)
Cod sursa(job #884527)
#include <fstream>
#include <vector>
#include <limits>
const int INF=std::numeric_limits<int>::max()/2;
int main(){
std::ifstream fin("royfloyd.in");
std::ofstream fout("royfloyd.out");
unsigned N; fin>>N;
std::vector<std::vector<int> > dist(N,std::vector<int>(N,INF));
for(unsigned i=0;i<N;++i)
for(unsigned j=0;j<N;++j){ int a; fin>>a; if(a!=0) dist[i][j]=a; }
for(unsigned i=0;i<N;++i) dist[i][i]=0;
for(unsigned k=0;k<N;++k)
for(unsigned i=0;i<N;++i)
for(unsigned j=0;j<N;++j)
if(dist[i][j]>dist[i][k]+dist[k][j])
dist[i][j]=dist[i][k]+dist[k][j];
for(unsigned i=0;i<N;++i){
for(unsigned j=0;j<N;++j)
if(dist[i][j]==INF) fout<<"0 ";
else fout<<dist[i][j]<<' ';
fout<<'\n';
}
}