Cod sursa(job #2394390)

Utilizator lucianistratiIstrati Lucian lucianistrati Data 1 aprilie 2019 16:41:26
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
#include <map>
const int INF=1e9;
using namespace std;

int N,i,j,a;
    int **cost;
void royFloyd()
{
    for(i=1;i<=N;i++)
    {
        cost[i][i]=0;
    }
    for(int k=1;k<=N;k++)
        for(int i=1;i<=N;i++)
           for(int j=1;j<=N;j++)
    {
        if(cost[i][j]>cost[i][k]+cost[k][j])
        {
            cost[i][j]=cost[i][k]+cost[k][j];
        }
    }
}
int main()
{

    ifstream fin("royfloyd.in");
    ofstream fout("royfloyd.out");
    fin>>N;
    cost=new int*[N];
    for(i=1;i<=N;i++)
    {
        cost[i]=new int[N];
    }
    for(i=1;i<=N;i++)
        for(j=1;j<=N;j++)
    {
        fin>>a;
        if(a==0)
        cost[i][j]=INF;
        else
        cost[i][j]=a;
    }
    royFloyd();
    for(i=1;i<=N;i++)
    {
        for(j=1;j<=N;j++)
    {
        fout<<cost[i][j]<<' ';
    }
    fout<<'\n';
    }
    fin.close();
    fout.close();
    return 0;
}