Cod sursa(job #2418138)

Utilizator Cezar211Popoveniuc Cezar Cezar211 Data 3 mai 2019 19:45:46
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 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();
        if(!viz[nod])
        {
            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});
                }
            viz[nod] = 1;
        }
    }
    for(int i=2; i<=n; i++)
        if(d[i] == INT_MAX)
            fout << 0 << ' ';
        else
            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});
    }
}