Cod sursa(job #2711659)

Utilizator Rares09Rares I Rares09 Data 24 februarie 2021 15:53:13
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>

using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");

int sum, nrsol, cos[50005], b[250005], c[250005];
vector <int> e[50005];
priority_queue <pair <int, int>, vector<pair <int, int> >, greater<pair <int, int> > > nodes;
bitset <50005> vis;
int main()
{
    int n, m;
    cin >> n >> m;

    for(int i = 1; i <= n; ++i)
        cos[i] = 1000000137;

    for(int i = 1; i <= m; ++i)
    {
        int a;
        cin >> a >> b[i] >> c[i];
        e[a].push_back(i);
    }

    nodes.push({0, 1});
    cos[1] = 0;

    while (!nodes.empty())
    {
        int i = nodes.top().second;
        nodes.pop();

        for(auto it = e[i].begin(); it != e[i].end(); ++it)
        {
            if(cos[b[*it]] > cos[i] + c[*it])
            {
                cos[b[*it]] = cos[i] + c[*it];
                nodes.push({cos[b[*it]], b[*it]});
            }
        }
    }

    for(int i = 2; i <= n; ++i)
    {
        if(cos[i] == 1e9 + 137)
            cos[i] = 0;

        cout << cos[i] << ' ';
    }

    return 0;
}