Pagini recente » Cod sursa (job #1477884) | Cod sursa (job #2171758)
#include <fstream>
#include <set>
#include <cstring>
using namespace std;
ifstream f("royfloyd.in");
ofstream g("royfloyd.out");
const int nmax = 102, INF = 0x3f3f3f3f;
int d[nmax];
int a[nmax][nmax];
int mat[nmax][nmax];
int n;
void dijkstra(int x){
memset(d, INF, sizeof d);
d[x] = 0;
set<pair<int, int> >h;
h.insert(make_pair(0,x));
while(!h.empty()){
int node = h.begin()->second;
h.erase(h.begin());
for(int i = 1; i <= n; ++i)
if(a[node][i] != 0){
int to = i;
int cost = a[node][i];
if(d[to] > d[node] + cost){
if(d[to] != INF)
h.erase(h.find(make_pair(d[to],to)));
d[to] = d[node] +cost;
h.insert(make_pair(d[to],to));
}
}
}
for(int i=1;i<=n;++i)
mat[x][i] = d[i];
}
int main(){
f>>n;
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
f>>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)
g<<mat[i][j]<<" ";
g<<'\n';
}
return 0;
}