Cod sursa(job #3176989)

Utilizator PiciuAndreiAlinPiciu Andrei Alin PiciuAndreiAlin Data 28 noiembrie 2023 10:40:39
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
#include <bits/stdc++.h>
#define oo 2e9
using namespace std;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
int top, a[105][105], d[105];
int n, m;
vector<pair<int, int>>L[101];
priority_queue<pair<int, int>>q;
bitset<101>viz;
void Dijkstra(int k)
{
    viz.reset();
    int i, cost;
    for(i = 1; i <= n; i++)
        d[i]= oo;
    q.push({0, k});
    d[k] = 0;
    while(!q.empty())
    {
        k = q.top().second;
        q.pop();
        if(viz[k] == 0)
        {
            viz[k] = 1;
            for(auto e : L[k])
            {
                i = e.second;
                cost = e.first;
                if(d[i] > d[k] + cost)
                {
                    d[i] = d[k] + cost;
                    q.push({-d[i], i});
                }
            }
        }
    }
}
void Citire()
{
    int i, j, c, k, x;
    fin >> n;
    for(i = 1; i <= n; i++)
    {
        for(j = 1; j <= n; j++)
        {
            fin >> x;
            if(x != 0)L[i].push_back({x, j});
        }
    }
    for(i = 1; i <= n; i++)
    {
        Dijkstra(i);
        for(j = 1; j <= n; j++)
        {
            if(d[j] == oo)a[i][j] = -1;
            else a[i][j] = d[j];
        }
    }
    for(i = 1; i <= n; i++)
    {
        for(j = 1; j <= n; j++)
            fout << a[i][j] <<  " ";
        fout << "\n";
    }
}

int main()
{
    Citire();
    return 0;
}