Cod sursa(job #2922222)

Utilizator tomaionutIDorando tomaionut Data 6 septembrie 2022 15:48:58
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <bits/stdc++.h>
#define INF 1e9
using namespace std;

ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, dp[50005], viz[50005];
vector <pair<int, int> > a[50005];
priority_queue <pair<int, int> > q;
void Dijkstra(int x)
{
    for (int i = 1; i <= n; i++)
        dp[i] = INF;
    dp[x] = 0;
    q.push({ 0, x });
    
    while (!q.empty())
    {
        x = q.top().second;
        q.pop();
        if (viz[x] == 0)
        {
            viz[x] = 1;
            for (auto w : a[x])
            {
                if (w.first + dp[x] < dp[w.second])
                {
                    dp[w.second] = w.first + dp[x];
                    q.push({ -dp[w.second], w.second });
                }
            }
        }
    }
}

int main()
{
    int i, x, y, c;
    fin >> n >> m;
    while (m--)
    {
        fin >> x >> y >> c;
        a[x].push_back({ c, y });
    }

    Dijkstra(1);
    for (i = 2; i <= n; i++)
        fout << dp[i] << " ";
    

    return 0;
}