Cod sursa(job #3213652)

Utilizator tomaionutIDorando tomaionut Data 13 martie 2024 12:32:10
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>
#define INF 2e9
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;
int main()
{
    int i, x, y, cost;
    fin >> n >> m;
    for (i = 1; i <= m; i++)
    {
        fin >> x >> y >> cost;
        a[x].push_back({y, cost});
        a[y].push_back({x, cost});
    }

    for (i = 1; i <= n; i++)
        dp[i] = INF;

    dp[1] = 0;
    Q.push({0, 1});
    while (!Q.empty())
    {
        x = Q.top().second;
        Q.pop();
        if (viz[x] == 0)
        {
            viz[x] = 1;
            for (auto w : a[x])
                if (dp[w.first] > w.second + dp[x])
                {
                    dp[w.first] = w.second + dp[x];
                    Q.push({-dp[w.first], w.first});
                }
        }
    }

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

    return 0;
}