Cod sursa(job #1976191)

Utilizator mitroi_stefan_danielMitroi Stefan-Daniel mitroi_stefan_daniel Data 2 mai 2017 21:26:53
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <iostream>
#include <fstream>
#include<time.h>
#define inf 1001

using namespace std;


int main(){

    int dist[101][101],n;
    ifstream in("royfloyd.in");
    ofstream out("royfloyd.out");

    in>>n;
    for(int i=1;i<=n;++i){
        for(int j=1;j<=n;++j){
            in>>dist[i][j];
            if(dist[i][j]==0 && i!=j)//nu avem dist de la i la j
                dist[i][j]=inf;
        }
    }
    clock_t t;
    t = clock();
    for(int k1=1;k1<=1;++k1){
    for(int k=1;k<=n;++k){//luam k drept nod intermediar
        for(int i=1;i<=n;++i)
            for(int j=1;j<=n;++j)
                if(dist[i][j]>dist[i][k]+dist[k][j] && dist[i][k]!=inf && dist[k][j]!=inf && i!=k && j!=k)//daca dist de la i la j prin k este mai mica
                    dist[i][j]=dist[i][k]+dist[k][j];//actualizam
    }
    }

    t = clock() - t;

   cout<<" execution time: "<<((double)t)/CLOCKS_PER_SEC<<endl;
   //cout<<" execution time: "<<(t2-t1);
    for(int i=1;i<=n;++i){
        for(int j=1;j<=n;++j){
            if(dist[i][j]==inf)
                out<<"0 ";
            else
                out<<dist[i][j]<<" ";
        }
        out<<"\n";
    }
    return 0;
}