Cod sursa(job #1726241)

Utilizator stefanchistefan chiper stefanchi Data 7 iulie 2016 16:10:56
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.36 kb
#include <iostream>
#include  <stdio.h>
#define N 105
using namespace std;
int n;
int cost[N][N];

void read()
{
    freopen("royfloyd.in","r",stdin);
    freopen("royfloyd.out","w",stdout);

    scanf("%d\n",&n);
    for(int i = 0 ; i < n ; i++)
        for(int j = 0 ; j < n ; j++)
        {
            scanf("%d ",&cost[i][j]);
            if(cost[i][j] == 0)
                cost[i][j] = 1010;
        }
}

void drum_minim()
{
    for(int k = 0 ; k < n ; k++)
    {
        for(int i = 0 ; i < n ; i++)
        {
            if(i != k)
            {
                for(int  j = 0 ; j < n ; j++)
                {
                    if(j != k)
                    {
                        if(cost[i][j] > cost[k][j] + cost[i][k])
                            cost[i][j] = cost[k][j] + cost[i][k];
                    }
                }
            }
        }
    }
}


void afisare()
{
    for(int i = 0 ; i <n ; i++)
    {
        for(int j = 0 ; j < n ; j++)
        {
            if(i == j)
                {printf("%d ", 0);
                 continue;}
            if(cost[i][j] == 1010)
                {printf("%d ", 0);
                 continue;}
             printf("%d ",cost[i][j]);
        }

        printf("\n");
    }
}


int main()
{
    read();
    drum_minim();
    afisare();
    return 0;
}