Cod sursa(job #2928037)

Utilizator moise_alexandruMoise Alexandru moise_alexandru Data 22 octombrie 2022 01:07:03
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
const int maxn = 50005;
vector <pair <int, int> > v[maxn];
int dist[maxn];
queue <int> q;
/// doar un test, nu e dijkstra
bitset <maxn> pus;
int main()
{
    int n, m;
    in >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        int x, y, c;
        in >> x >> y >> c;
        v[x].push_back(make_pair(y, c));
    }
    for(int i = 2; i <= n; i++)
        dist[i] = (1 << 30);
    q.push(1);
    pus[1] = 1;
    while(!q.empty())
    {
        int nod = q.front();
        q.pop();
        pus[nod] = 0;
        for(auto it : v[nod])
        {
            if(dist[it.first] > dist[nod] + it.second)
            {
                dist[it.first] = dist[nod] + it.second;
                if(!pus[it.first])
                    q.push(it.first);
            }
        }
    }
    for(int i = 2; i <= n; i++)
    {
        if(dist[i] == (1 << 30))
            out << 0 << " ";
        else
            out << dist[i] << " ";
    }
    return 0;
}