Cod sursa(job #2323134)

Utilizator filicriFilip Crisan filicri Data 18 ianuarie 2019 21:14:52
Problema Algoritmul lui Dijkstra Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <fstream>
#include <vector>
#include <queue>
#define mmax 250004
#define nmax 50004
#define inf 99999999
#define f first
#define s second
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n,m,dist[nmax],a,b,c;
bool seen[nmax];
vector <pair<int,int> > G[mmax];
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({-c,b});
    }
    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 ne:G[node])
            if (dist[node]-ne.f<dist[ne.s])
            {
                dist[ne.s]=dist[node]-ne.f;
                pq.push({-dist[ne.s],ne.s});
            }
    }
    for (int i=2;i<=n;i++)
        if (dist[i]==inf) g<<0<<' ';
        else g<<dist[i]<<' ';
    f.close();
    g.close();
    return 0;
}