Cod sursa(job #2770904)

Utilizator Tudor06MusatTudor Tudor06 Data 24 august 2021 00:07:55
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <cstring>

using namespace std;

const int NMAX = 5e4;

priority_queue <pair<int,int>> q;
vector <pair<int,int>> edges[NMAX + 1];

int dist[NMAX + 1];

void dijkstra( int node ) {
    q.push( {0,node} );

    while ( !q.empty() ) {
        node = q.top().second;
        int d = q.top().first;
        q.pop();

        if ( dist[node] == -1 ) {
            dist[node] = -d;
            for ( auto i : edges[node] ) {
                if ( dist[i.second] == -1 ) {
                    q.push( {d - i.first,i.second} );
                }
            }
        }
    }
}
int main() {
    ifstream fin( "dijkstra.in" );
    ofstream fout( "dijkstra.out" );
    int n, i, m, a, b, c;
    fin >> n >> m;
    for ( i = 0; i < m; i ++ ) {
        fin >> a >> b >> c;
        edges[a].push_back( {c,b} );
//        edges[b].push_back( {a,c} );
    }
    memset( dist, -1, sizeof(dist) );
    dijkstra( 1 );
    for ( i = 2; i <= n; i ++ ) {
        fout << max( 0, dist[i] ) << ' ';
    }
    return 0;
}