Pagini recente » Cod sursa (job #1423160) | Cod sursa (job #484248) | Cod sursa (job #819648) | Cod sursa (job #1989959) | Cod sursa (job #1689707)
#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
#include <climits>
#define MAX 103
using namespace std;
ifstream in("royfloyd.in");
ofstream out("royfloyd.out");
vector<pair<int, int>> vec[MAX];
int mat[MAX][MAX];
int n,v;
void bell(int src) {
queue<int> que;
for(int i = 1; i <= n; i++)
mat[src][i] = INT_MAX/2-1;
mat[src][src] = 0;
que.push(src);
int nod;
vector<pair<int, int>>::iterator it;
while(!que.empty()) {
nod = que.front();
que.pop();
for(it = vec[nod].begin(); it != vec[nod].end(); it++) {
if(mat[src][nod]+it->second < mat[src][it->first]) {
mat[src][it->first] = mat[src][nod]+it->second;
que.push(it->first);
}
}
}
}
int main() {
in >> n;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
in >> v;
if(v != 0)
vec[i].push_back(make_pair(j, v));
}
}
for(int i = 1; i <= n; i++)
bell(i);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
out << mat[i][j] << " ";
}
out << '\n';
}
return 0;
}