Pagini recente » Cod sursa (job #1023128) | Cod sursa (job #3199268) | Cod sursa (job #1323221) | Cod sursa (job #3183650) | Cod sursa (job #1612771)
#include<fstream>
#include<queue>
using namespace std;
ifstream in("royfloyd.in");
ofstream out("royfloyd.out");
int A[110][110],N;
typedef pair<int, int> Node;
#define p first
#define c second
#define mk make_pair
#define INF (1<<30)
struct Comparator
{
bool operator () (const Node &e1, const Node &e2)
{
return e1.c > e2.c;
}
};
int D[110][110];
priority_queue<Node, vector<Node>, Comparator> PQ;
void Dijkstra(int S)
{
for (int i = 1; i <= N; ++i)
D[S][i] = INF;
D[S][S] = 0;
PQ.push(mk(S, 0));
while (PQ.size())
{
Node node = PQ.top();
PQ.pop();
if (D[S][node.p] < node.c)
continue;
for (int i = 1; i<=N; ++i)
{
if (A[node.p][i]!=0 && node.c + A[node.p][i] < D[S][i])
{
D[S][i] = node.c + A[node.p][i];
PQ.push(mk(i, D[S][i]));
}
}
}
}
int main()
{
in >> N;
for (int i = 1;i <= N;++i)
for (int j = 1;j <= N;++j)
in >> A[i][j];
for (int i = 1;i <= N;++i)
Dijkstra(i);
for (int i = 1;i <= N;++i)
{
for (int j = 1;j <= N;++j)
out << D[i][j] << " ";
out << '\n';
}
}