Cod sursa(job #1705546)

Utilizator roxanaraduRoxana Radu roxanaradu Data 20 mai 2016 19:23:18
Problema Floyd-Warshall/Roy-Floyd Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <climits>
#define NMax 1100
using namespace std;

void Floyd_Warshal(int n, int Cost[][NMax] ) {

    int k, i , j;
    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];
                    
                }
            }

}

int main()
{
    ifstream f("royfloyd.in");
    ofstream g("royfloyd.out");

    int n, m, c;

    int u, v, i, j;

    f >> n ;


    int cost[NMax][NMax];


    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] = INT_MAX / 5;

        }



    Floyd_Warshal(n, cost);


    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++)
            if (cost[i][j] < INT_MAX/5)
                g << cost[i][j] << " ";
            else
                g << "0 ";
        g << "\n";
    }

}