Cod sursa(job #2964315)

Utilizator octavi26octavian octavi26 Data 12 ianuarie 2023 19:57:58
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include <bits/stdc++.h>
#define N 50004
#define inf 1e9

using namespace std;

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

int n, m;
int z;
vector< pair<int, int> > g[N];
priority_queue< pair<int, int>, vector< pair<int, int> > > q;
int D[N];

void Citire()
{
    int i;
    int x, y, c;
    fin >> n >> m;
    z = 1;
    for( i=1; i<=m; i++ )
    {
        fin >> x >> y >> c;
        g[x].push_back( {y, c} );
    }
}

void Dijkstra( int x )
{
    int i;
    q.emplace( make_pair(0, x) );
    D[x] = 0;
    //S[x] = 1;
    for( i=1; i<=n; i++ )
        if( i != x ) D[i] = inf;
    while( !q.empty() )
    {
        int nod = q.top().second;
        int c = -q.top().first;
        q.pop();
        //S[nod] = 1;
        for( auto y : g[nod] )
        {
            if( c + y.second < D[y.first] )
            {
                D[y.first] = c + y.second;
                //T[y.first] = nod;
                q.emplace( make_pair( -D[y.first], y.first ) );
            }
        }
    }
}

void Rezolvare()
{
    int i;
    for (i = 2; i <= n; i++)
        if (D[i] == inf) fout << "0 ";
        else fout << D[i] << " ";
}

int main()
{
    Citire();
    Dijkstra(z);
    Rezolvare();
    return 0;
}