Cod sursa(job #3212682)

Utilizator PiciuAndreiAlinPiciu Andrei Alin PiciuAndreiAlin Data 12 martie 2024 08:44:02
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <bits/stdc++.h>
#define t 10e9
using namespace std;

ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m;
long long d[5005];
bitset<50005>viz;
vector<pair<int, int>>L[50005];
priority_queue<pair<long long, int>>q;
void Dijkstra(int k)
{
    int i;
    for(i = 1; i <= n; i++)
        d[i] = t;
    d[k] = 0;
    q.push({0, k});
    while(!q.empty())
    {
        k = q.top().second;
        q.pop();
        if(viz[k] == 0)
        {
            viz[k] = 1;
            for(auto i : L[k])
            {
                if(d[i.first] > d[k] + i.second)
                {
                    d[i.first] = d[k] + i.second;
                    q.push({-d[i.first], i.first});
                }
            }
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(0);
    fin.tie(0);
    fout.tie(0);
    int i, x, y, c;
    fin >> n >> m;
    for(i = 1; i <= m; i++)
    {
        fin >> x >> y >> c;
        L[x].push_back({y, c});
        L[y].push_back({x, c});
    }
    Dijkstra(1);
    for(i = 2; i <= n; i++)
        fout << d[i] << " ";
    return 0;
}