Pagini recente » Cod sursa (job #2114125) | Cod sursa (job #2349589) | Cod sursa (job #1409473) | Cod sursa (job #2651721) | Cod sursa (job #1974615)
#include <iostream>
#include <fstream>
using namespace std;
const int nMax = 105;
const int INF = (1 << 26);
int n;
int cost[nMax][nMax];
int dp[2][nMax][nMax];
void citire()
{
ifstream in("royfloyd.in");
in >> n;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
{
in >> cost[i][j];
if(i != j && cost[i][j] == 0)
cost[i][j] = INF;
}
in.close();
}
void rezolvare()
{
int current = 0, last = 1;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
dp[current][i][j] = cost[i][j];
for(int k = 1; k <= n; ++k)
{
swap(current, last);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
{
dp[current][i][j] = min(dp[last][i][j], dp[last][i][k] + dp[last][k][j]);
}
}
ofstream out("royfloyd.out");
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= n; ++j)
out << dp[current][i][j] << " ";
out << "\n";
}
out.close();
}
int main()
{
citire();
rezolvare();
return 0;
}