Cod sursa(job #3251918)

Utilizator DARIUSQSSDarius Nicoara DARIUSQSS Data 27 octombrie 2024 19:07:43
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <bits/stdc++.h>
#include <cstdio>
#include <queue>

using  namespace std;

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

#define inf 1000000000

int n, m;
//// node val
vector<pair<long long,int>> adj[6];
bitset<6> viz;
priority_queue<pair<long long, int>> pq;

int main()
{
    fin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        int x, y, c;
        fin >> x >> y >> c; 
        adj[x].push_back({y, c});
    }
    

    vector<long long> dist(n + 1, inf);
    pq.push({1, 0});
    dist[1] = 0;

    while(!pq.empty())
    {
        int a = pq.top().first; pq.pop();
        if(viz[a]) continue;
        viz[a] = 1;
        for(auto x : adj[a])
        {
            int b = x.first, w = x.second;
            if(dist[a] + w < dist[b])
            {
                dist[b] = dist[a] + w;
                pq.push({b, -dist[b]});
            }
        }
    }

    for(int i = 2; i <= n; i++)
    {
        fout << dist[i] << ' ';
    }

}