Cod sursa(job #1705565)

Utilizator alex95panPandelea Alexandru alex95pan Data 20 mai 2016 19:37:54
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.77 kb
#include<iostream>
#include<fstream>
#include <climits>
#define INF 696969
using namespace std;
int prec[100][100];
int cost[100][100];
bool path[100][100];

void drum(int x,int y)
{
    if(x!=y)
        drum(x,prec[x][y]);
    cout<<y<<" ";
}

void roy_floyd(int n){
    int i,j,k;

     for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            if(i==j || cost[i][j]==INF)
                prec[i][j]=0;
            else
                prec[i][j]=i;

    for(k=1;k<=n;k++)
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
                if(cost[i][j]>cost[i][k]+cost[k][j])
                {
                    cost[i][j]=cost[i][k]+cost[k][j];
                    prec[i][j]=prec[k][j];
                }
}

void print(int n){
    int i,j;
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            if(i!=j && cost[i][j]<INF)
            {
                cout<<"Costul de la "<<i<<" la "<<j<<" este "<<cost[i][j]<<" cu drumul: ";
                drum(i,j);
                cout<<endl;
            }
}

int main(void)
{
    int i,j,m,n,x,y,k;
    fstream f,out;
    f.open("royfloyd.in",ios::in);
    out.open("royfloyd.out",ios::out);
    f>>n;
    //cout<<n<<" "<<m<<endl;
    /*for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            if(i!=j)
                cost[i][j]=INF;*/
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
        {
            f>>cost[i][j];

             if (i != j && cost[i][j] == 0)
                cost[i][j] = INF;
        }

    roy_floyd(n);

    //calea efectiva
    //print(n);

    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
            if (cost[i][j] < INF)
                out << cost[i][j] << " ";
            else
                out << "0 ";
        out<<endl;
    }
}