Cod sursa(job #2490709)

Utilizator iulianarsenoiuArsenoiu Iulian iulianarsenoiu Data 10 noiembrie 2019 18:48:16
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.47 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;
ifstream f("royfloyd.in");
ofstream g("royfloyd.out");
typedef pair<int,int> graf;
const int INF = 0x3f3f3f3f;
int n,i,j,x,r[105][105],d[105];
bool sel[105];
vector <graf> G[1005];
void Dijkstra(int pl)
{
    priority_queue <graf,vector<graf>,greater<graf>> h;
    for(int i=1; i<=n; i++)
    {
        d[i]=INF;
        sel[i]=false;
    }
    sel[pl]=true;
    d[pl]=0;
    for(auto it : G[pl])
    {
        h.push(it);
        d[it.second]=it.first;
    }
    while(!h.empty())
    {
        while(!h.empty() && sel[h.top().second])
            h.pop();
        if(h.empty())
            break;
        graf k=h.top();
        h.pop();
        sel[k.second]=true;
            for(auto it : G[k.second])
        {
            if(k.first+it.first<d[it.second])
            {
                d[it.second]=k.first+it.first;
                h.push({d[it.second],it.second});
            }
        }
    }
    for(int i=1; i<=n; i++)
        r[pl][i]=d[i];
}
void Write()
{
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
            g<<r[i][j]<<' ';
        g<<'\n';
    }
}
int main()
{
    f>>n;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=n; j++)
        {
            f>>x;
            if(x!=0)
                G[i].push_back({x,j});
        }
    }
    for(i=1; i<=n; i++)
        Dijkstra(i);
    Write();
    return 0;
}