Cod sursa(job #1757549)

Utilizator DoubleNyNinicu Cristian DoubleNy Data 15 septembrie 2016 12:22:51
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <bits/stdc++.h>
#define ll long long
#define lli long long int
#define endl "\n"
#include <stdio.h>
using namespace std;
int cost[300][300];
int n;
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] != 0 && cost[k][j] !=0 && (cost[i][j] >= cost[i][k] + cost[k][j] || cost[i][j] == 0))
                    {
                        cost[i][j] = cost[i][k] + cost[k][j];
                    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    ifstream fin("royfloyd.in");
    ofstream fou("royfloyd.out");
    fin >> n;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            fin >> cost[i][j];
        }
    RoyFloyd();
    for(int i=1;i<=n;i++)
    {
       for(int j=1;j<=n;j++)
            fou << cost[i][j] << ' ';
        fou << endl;
    }
    return (0);
}