Cod sursa(job #2669136)

Utilizator bogdan2604Bogdan Dumitrescu bogdan2604 Data 6 noiembrie 2020 10:41:52
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.46 kb
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#define inf 0x3f3f3f3f

using namespace std;

ifstream f("dijkstra.in");
ofstream g("dijkstra.out");

auto cmp = [] (pair <int,int> a, pair <int,int> b)
{
  return a.second > b.second;
};
int n,m,x,y,a,b,i,pret,cost[100001];
bitset <100001> trecut;
vector <vector<pair<int,int>>> drumuri;
priority_queue<pair<int,int>,vector<pair<int,int>>,decltype(cmp)> pq(cmp);

int main()
{
    ios::sync_with_stdio(0);
    f >> n >> m;
    drumuri.resize(n + 1);
    for(i = 0; i < n; ++ i)
    {
        f >> x >> y >> pret;
        drumuri[x].push_back({y,pret});
    }
    for(i = 2; i <= n; ++ i)
        cost[i] = inf;
    for(i = 0; i < drumuri[1].size(); ++ i)
    {
        a = drumuri[1][i].first;
        b = drumuri[1][i].second;
        cost[a] = b;
        pq.push({a,b});
    }
    trecut[1] = 1;
    while(!pq.empty())
    {
        a = pq.top().first;
        b = pq.top().second;
        pq.pop();
        if(trecut[a])
            continue;
        trecut[a] = 1;
        for(i = 0; i < drumuri[a].size(); ++ i)
        {
            x = drumuri[a][i].first;
            y = drumuri[a][i].second;
            if(cost[x] > y + b)
            {
                cost[x] = y + b;
                pq.push({x,y + b});
            }
        }
    }
    for(i = 2; i <= n; ++ i)
        if(!trecut[i])
        g << 0 << ' ';
    else
        g << cost[i] << ' ';
}