Cod sursa(job #2711629)

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

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

int sum, nrsol, cos[250005];
vector <pair <int, 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] = 1e9;

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

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

    for(int w = 1; w < n; ++w)
    {
        int i = nodes.top().second;
        vis[i] = true;
        nodes.pop();

        for(auto it = e[i].begin(); it != e[i].end(); ++it)
        {
            if(cos[it->second] > cos[i] + it->first && vis[it->second] == false)
            {
                cos[it->second] = cos[i] + it->first;
                nodes.push({cos[it->second], it->second});
            }
        }
    }

    for(int i = 2; i <= n; ++i)
    {
        cout << cos[i] << ' ';
    }

    return 0;
}