Cod sursa(job #2479943)

Utilizator uvIanisUrsu Ianis Vlad uvIanis Data 24 octombrie 2019 18:18:22
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.65 kb
#include <bits/stdc++.h>
#define NMAX 101
using namespace std;

int n;
vector<vector<pair<int, int>>> graph(NMAX, vector<pair<int, int>>());

vector<int> dist(NMAX);
vector<int> inPQ(NMAX, false);

void minDist(int source)
{
    fill(dist.begin(), dist.end(), 2000000000);

    struct compare_nodes{
        bool operator() (int node1, int node2)
        {
            return node1 > node2;
        }
    };

    priority_queue<int, vector<int>, compare_nodes> PQ;

    dist[source] = 0;

    PQ.push(source);

    while(!PQ.empty())
    {
        int node = PQ.top();
        PQ.pop();

        inPQ[node] = false;

        for(pair<int, int>& next : graph[node])
        {
            if(dist[node] + next.second < dist[next.first])
            {
                dist[next.first] = dist[node] + next.second;

                if(inPQ[next.first] == false)
                {
                    inPQ[next.first] = true;
                    PQ.push(next.first);
                }
            }
        }
    }
}


int main()
{
    ios_base::sync_with_stdio(false);

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


    fin >> n;

    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= n; ++j)
        {
            int cost;

            fin >> cost;

            if(cost) graph[i].push_back(make_pair(j, cost));
        }
    }

    for(int i = 1; i <= n; ++i)
    {
        minDist(i);

        for(int j = 1; j <= n; ++j)
        {
            if(dist[j] == 2000000000) fout << 0 << ' ';
            else fout << dist[j] << ' ';
        }

        fout << '\n';
    }
}