Cod sursa(job #2175747)

Utilizator hertzalauPOPESCU ION hertzalau Data 16 martie 2018 18:50:48
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.55 kb
#include <bits/stdc++.h>

/// !!!! neighbour nu neighbourgh

using namespace std;

ifstream in("royfloyd.in");
ofstream out("royfloyd.out");

int n, m, visited[105], x, y, v, start, i, j, inf = 100005, p;
struct e
{
    int number_of_neighbourghs;
    vector < int > neighbourghs;
    vector < int > price;
}vertices[105];
queue < int > Q;


void run_algorithm()
{

    start = p;
    visited[start] = 0;
    Q.push(start);
    while(!Q.empty())
    {
        x = Q.front();
        Q.pop();
        for(i = 0; i < vertices[x].neighbourghs.size(); i++)
        {
            if(visited[x] + vertices[x].price[i] < visited[vertices[x].neighbourghs[i]])
            {
                visited[vertices[x].neighbourghs[i]] = visited[x] + vertices[x].price[i];
                Q.push(vertices[x].neighbourghs[i]);
            }
        }
         ///outData();
    }

}

int main()
{
    in >> n;
    for(i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            in >> v;
            if(v != 0)
            {
                  vertices[i].number_of_neighbourghs++;
                   vertices[i].neighbourghs.push_back(j);
                    vertices[i].price.push_back(v);
            }
        }
    }
    for(p = 1; p <= n; p++)
    {
        for(i = 1; i <= n; i++)
        visited[i] = inf;
    run_algorithm();
    for(int i = 1; i <= n; i++)
        if(visited[i] == inf)
        out << "0 ";
        else
        out << visited[i] << " ";
        out <<"\n";
    }

    return 0;
}