Cod sursa(job #2175787)

Utilizator UrsuDanUrsu Dan UrsuDan Data 16 martie 2018 18:59:41
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
#include <cstdio>
#include <vector>
#include <queue>

using namespace std;

const int DIST_MAX = 1000000000;
const int N_MAX = 50000;

struct nodes
{
    int node;
    int dist;
    const bool operator < (const nodes &other)const
    {
        return dist > other.dist;
    }
};

priority_queue<nodes> pq;
vector<nodes> g[N_MAX + 1];

int dist[N_MAX + 1];

int main()
{
    freopen("dijkstra.in", "r", stdin);
    freopen("dijkstra.out", "w", stdout);
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; i++)
    {
        int x, y, c;
        scanf("%d%d%d", &x, &y, &c);
        g[x].push_back({y, c});
    }
    for(int i = 2; i <= n; i++)
        dist[i] = DIST_MAX;
    pq.push({1, 0});
    while(pq.empty() == false)
    {

        nodes t = pq.top();
        pq.pop();
        int l = g[t.node].size();
        for(int i = 0; i < l; i++)
        {
            nodes son = g[t.node][i];
            if(dist[t.node] + son.dist < dist[son.node])
            {
                dist[son.node] = dist[t.node] + son.dist;
                pq.push({son.node, dist[son.node]});
            }
        }
    }
    for(int i = 2; i <= n; i++)
    {
        if(dist[i] == DIST_MAX)
            printf("0 ");
        else
            printf("%d ", dist[i]);
    }
    printf("\n");
    return 0;
}