Pagini recente » Cod sursa (job #1221007) | Cod sursa (job #2802121) | Cod sursa (job #2506873) | Cod sursa (job #324493) | Cod sursa (job #1689709)
#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
#include <climits>
#define MAX 103
using namespace std;
ifstream in("royfloyd.in");
ofstream out("royfloyd.out");
int cost[MAX][MAX];
int dist[MAX][MAX];
int n,v;
void bell(int src) {
queue<int> que;
for(int i = 1; i <= n; i++)
dist[src][i] = INT_MAX/2-1;
dist[src][src] = 0;
que.push(src);
int nod;
vector<pair<int, int>>::iterator it;
while(!que.empty()) {
nod = que.front();
que.pop();
for(int i = 1; i <= n; i++) {
if(cost[nod][i] == 0)
continue;
if(dist[src][nod]+cost[nod][i] < dist[src][i]) {
dist[src][i] = dist[src][nod]+cost[nod][i];
que.push(i);
}
}
}
}
int main() {
in >> n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
in >> cost[i][j];
for(int i = 1; i <= n; i++)
bell(i);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++)
out << dist[i][j] << " ";
out << '\n';
}
return 0;
}