Pagini recente » Borderou de evaluare (job #2001510) | Cod sursa (job #2515905) | Cod sursa (job #511160) | Cod sursa (job #2647905) | Cod sursa (job #1967319)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream in("royfloyd.in");
ofstream out("royfloyd.out");
typedef long long ll;
const int NMax = 105;
const int inf = 1e9 + 5;
int N;
int dist[NMax][NMax][NMax];
// dist[i][j][k] = distanta minima intre nodurile i si j,
// folosind pe drumul dintre ele noduri din multimea {1,2,3,...,k}
// insa nu este necesara retinerea tutoror starilor k
// din moment ce pentru obtinerea unei stari k este necesara doar starea precedenta (k-1)
// vezi submisia urmatoare
int main() {
in>>N;
for (int i=1;i<=N;++i) {
for (int j=1;j<=N;++j) {
in>>dist[i][j][0];
if (!dist[i][j][0] && i!=j) {
dist[i][j][0] = inf;
}
}
}
for (int k=1;k<=N;++k) {
for (int i=1;i<=N;++i) {
for (int j=1;j<=N;++j) {
dist[i][j][k] = min(dist[i][j][k-1],dist[i][k][k-1] + dist[k][j][k-1]);
}
}
}
for (int i=1;i<=N;++i) {
for (int j=1;j<=N;++j) {
out<<((dist[i][j][N] == inf) ? 0 : dist[i][j][N])<<' ';
}
out<<'\n';
}
in.close();out.close();
return 0;
}