Cod sursa(job #2650740)

Utilizator popoviciAna16Popovici Ana popoviciAna16 Data 19 septembrie 2020 22:08:40
Problema Algoritmul lui Dijkstra Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <fstream>
#include <vector>
#include <set>
using namespace std;

ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");

vector <pair <int, int>> v[50001];

long long d[50001];

struct el
{
    int nod;
    bool operator <(const el &alt) const
    {
        return d[nod] < d[alt.nod];
    }
};

set <el> s;

int main()
{
    int n, m, i, j, x, y, z;
    fin >> n >> m;
    for (i = 1; i<=m; i++)
    {
        fin >> x >> y >> z;
        v[x].push_back({y, z});
    }
    for (i = 1; i<=n; i++)
        d[i] = 1ll<<50;
    d[1] = 0;
    s.insert({1});
    for (i = 1; s.empty() == 0 && i<n; i++)
    {
        x = s.begin()->nod;
        s.erase({x});
        for (j = 0; j<v[x].size(); j++)
            if (d[v[x][j].first] > d[x] + v[x][j].second)
            {
                s.erase({v[x][j].first});
                d[v[x][j].first] = d[x] + v[x][j].second;
                s.insert({v[x][j].first});
            }
    }
    for (i = 2; i<=n; i++)
    {
        if (d[i] < (1ll<<50))
            fout << d[i] << ' ';
        else
            fout << "0 ";
    }
    return 0;
}