Cod sursa(job #803531)

Utilizator TeodoraTanaseTeodora Tanase TeodoraTanase Data 27 octombrie 2012 19:12:11
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include <cstdio>

#define NMAX 101

using namespace std;

FILE *inFile = fopen ("royfloyd.in", "r");
FILE *outFile = fopen ("royfloyd.out", "w");

int n;
int cost[NMAX][NMAX];

void read()
{
    fscanf (inFile, "%d\n", &n);

    for (int i = 1; i <= n; ++ i)
        for (int j = 1; j <= n; ++ j)
            fscanf (inFile, "%d ", &cost[i][j]);
}

void royfloyd()
{
    for (int k = 1; k <= n; ++ k)
        for (int i = 1; i <= n; ++ i)
            for (int j = 1; j <= n; ++ j)
                if (i != j && cost[i][k] + cost[k][j] < cost[i][j])
                    cost[i][j] = cost[i][k] + cost[k][j];
}

void write()
{
    for (int i = 1; i <= n; ++ i)
    {
        for (int j = 1; j <= n; ++ j)
            fprintf (outFile, "%d ", cost[i][j]);

        fprintf (outFile, "\n");
    }
}

int main()
{
    read();
    royfloyd();
    write();

    return 0;
}