Cod sursa(job #2871687)

Utilizator tallianfranciskaFranciska Tallian tallianfranciska Data 15 martie 2022 14:44:29
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.09 kb
// dijkstra.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <fstream>
#include <vector>
#include <queue>
#include <climits>

#define ll long long

using namespace std;

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


struct ut
{
    ll hova, hossz;

};
priority_queue<ut>que;
ut akt;
bool operator<(const ut& a, const ut& b)
{
    return a.hossz > b.hossz;
}
struct adat
{
    bool lat = false;
    ll hossz = LLONG_MAX;
    vector < pair<ll, ll>>sz;
};

vector<adat>x;
ll n, m, a, b, c,i;

int main()
{
    cin >> n >> m;
    x.resize(n + 1);

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

    }
    que.push({1,0});
    while (!que.empty())
    {
        akt = que.top();
        while (x[akt.hova].lat && !que.empty())
        {
            que.pop();
            if (!que.empty())akt = que.top();
        }
        if (que.empty())break;
        que.pop();
        x[akt.hova].lat = true;
        x[akt.hova].hossz = akt.hossz;
        for (auto& e : x[akt.hova].sz)
        {
            if (!x[e.first].lat && x[e.first].hossz > (akt.hossz + e.second))
            {
                x[e.first].hossz=akt.hossz + e.second;
                que.push({ e.first , akt.hossz + e.second });
            }
        }
    }
    for(i=2;i<=n;++i)
        cout << x[i].hossz << " ";
    return 0;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file