Cod sursa(job #2418136)

Utilizator Cezar211Popoveniuc Cezar Cezar211 Data 3 mai 2019 19:43:08
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>
#define NM 50005
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
void read();
void dijkstra();
bitset<NM> viz;
vector<pair<int,int>> v[NM];
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> q;
int n, m, d[NM];
int main()
{
    read();
    dijkstra();
    return 0;
}
void dijkstra()
{
    for(int i=1; i<=n; i++)
        d[i] = INT_MAX;
    d[1] = 0;
    q.push({0, 1});
    while(!q.empty())
    {
        int nod = q.top().second;
        q.pop();
        for(auto it : v[nod])
            if(!viz[it.first] && d[nod]+it.second < d[it.first])
            {
                d[it.first] = d[nod]+it.second;
                q.push({d[it.first], it.first});
            }
    }
    for(int i=2; i<=n; i++)
        fout << d[i] << ' ';
}
void read()
{
    int x, y, c;
    fin >> n >> m;
    for(int q=1; q<=m; ++q)
    {
        fin >> x >> y >> c;
        v[x].push_back({y, c});
    }
}