Cod sursa(job #2871125)

Utilizator mateisebastian4Matei Sebastian mateisebastian4 Data 12 martie 2022 21:30:17
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
const int N=12356365,nr=12351551;
int dp[N],n,m,x,y,cost;

struct pereche {
int y,cost;
};

struct comparare
{
    bool operator()(pereche A,pereche B)
    {
        return dp[A.y]>dp[B.y];
    }
};

vector <pereche>v[N];
bitset<N>inq;
priority_queue<pereche,vector<pereche>,comparare>q;
void cit()
{
  fin>>n>>m;
  pereche w;
  while(fin>>x>>y>>cost)
  {
      w.y=y;
      w.cost=cost;
      v[x].push_back(w);
  }

}

void init(int n)
{
    for(int i=1;i<=n;i++)
        dp[i]=nr;
}
void bfs()
{
    inq[1]=true;
    pereche x;
    x.y=1;
    x.cost=0;
    dp[1]=0;
    q.push(x);

    while(!q.empty())
    {
        x=q.top();
        q.pop();
        inq[x.y]=false;
        for(auto y:v[x.y])
        {
            if(dp[y.y]>dp[x.y]+y.cost)
            {
                dp[y.y]=dp[x.y]+y.cost;
                if(!inq[y.y])
                {
                    inq[y.y]=true;
                    q.push(y);
                }
            }
        }
    }
}



int main()
{
    cit();
    init(n+1);
    bfs();
    for(int i=2;i<=n;i++){
        if(dp[i]==nr){
            fout<<0<<" ";
            continue;
        }
    fout<<dp[i]<<" ";
}
}