Cod sursa(job #2533570)

Utilizator filicriFilip Crisan filicri Data 29 ianuarie 2020 12:38:05
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <fstream>
#include <vector>
#include <queue>
#define nmax 50004
#define inf 5000000004
#define f first
#define s second
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");

int n, m, a, b, c, dist[nmax];
bool seen[nmax];
vector<pair<int, int> > G[nmax];
priority_queue<pair<int, int> > pq;

int main()
{
    f>>n>>m;
    for(int i=1;i<=m;i++) {
        f>>a>>b>>c;
        G[a].push_back({b, c});
    }

    for(int i=2;i<=n;i++)
        dist[i]=inf;

    pq.push({0, 1});
    while(!pq.empty()) {
        int node=pq.top().s;
        pq.pop();
        if(seen[node])
            continue;
        seen[node]=1;
        for(auto i:G[node])
            if(dist[node]+i.s<dist[i.f]) {
                dist[i.f]=dist[node]+i.s;
                pq.push({-dist[i.f], i.f});
            }
    }

    for(int i=2;i<=n;i++)
        if(dist[i]==inf)
            g<<0<<' ';
        else
            g<<dist[i]<<' ';

    f.close();
    g.close();
    return 0;
}