Cod sursa(job #3202382)

Utilizator Costi2mFulina Costin Costi2m Data 11 februarie 2024 14:19:45
Problema Floyd-Warshall/Roy-Floyd Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.56 kb
#include <bits/stdc++.h>

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

int N, inf=1000000, distanta[1001], vizitat[1001];
vector<pair<int, int>> v[1001];
priority_queue<pair<int, int>> q;

int main()
{
    int i=1, j=1;
    fin >> N;
    while(i<=N)
    {
        j=1;
        while(j<=N)
        {
            int w;
            fin >> w;
            if(w) v[i].push_back({j, w});
            j++;
        }
        i++;
    }
    for(int start=1; start<=N; start++)
    {
        // memset(*distanta, inf, N+1);
        for(int h=1; h<=N; h++) distanta[h] = inf;
        // memset(*vizitat, 0, N+1);
        for(int h=1; h<=N; h++) vizitat[h] = 0;
        distanta[start]=0;
        q.push({start, 0});
        while(!q.empty())
        {
            int close=q.top().first;
            q.pop();
            if(vizitat[close]==0)
            {
                vizitat[close]=1;
                for(auto element: v[close])
                {
                    int nod = element.first;
                    int greutate = element.second;
                    if(distanta[nod] > distanta[close] + greutate)
                    {
                        distanta[nod] = distanta[close] + greutate;
                        q.push({nod, -distanta[nod]});
                    }
                }
                
            }
        }
        for(int k=1; k<=N; k++)
        {
            if((distanta[k]!=0) && (distanta[k]<inf)) fout << distanta[k];
            else fout << 0;
            fout << " ";
        }
        fout << "\n";
    }
    return 0;
}