Cod sursa(job #2753207)

Utilizator Fantastic_Mantudor voicu Fantastic_Man Data 21 mai 2021 15:42:21
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <fstream>
#include <vector>
#include <queue>
#define to first
#define cost second
using namespace std;
const int NMAX = 5e4;
const int MMAX = 25e4;
const int oo = 1e9;
ifstream fin ( "dijkstra.in" );
ofstream fout ( "dijkstra.out" );
vector < pair < int, int > > g[MMAX + 1];
int dist[NMAX + 1];
int n;
bool viz[NMAX + 1];
void dijkstra ( int node ) {
    priority_queue < pair < int, int > > pq;
    for ( int i = 1; i <= n; i++ )
        dist[i] = oo;
    dist[node] = 0;
    pq.push ( { 0, node } );
    while ( ! pq.empty () ) {
        int curr = pq.top().second;
        pq.pop ();
        if ( ! viz[curr] ) {
            viz[curr] = 1;
            for ( auto x: g[curr] ) {
                if ( dist[curr] + x.cost < dist[x.to] ) {
                    dist[x.to] = dist[curr] + x.cost;
                    pq.push ( { -dist[x.to], x.to } );
                }
            }
        }
    }
}
int main () {
    int m, x, y, c, poz, min = 1e9;
    fin >> n >> m;
    for ( int i = 1; i <= m; i++ ) {
        fin >> x >> y >> c;
        g[x].push_back ( { y, c } );
    }
    dijkstra ( 1 );
    for ( int i = 2; i <= n; i++ )
        fout << ( dist[i] == oo? 0: dist[i] ) << ' ';
    
    return 0;
}