Pagini recente » Istoria paginii runda/hlo-2023-lot-tema0/clasament | Cod sursa (job #2866833)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int N=5e4 +5,nr=2314155661;
void init(n)
{
for(int i=1;i<=n;i++)
dp[i]=nr;
}
void cit()
{
int x,y,cost;
pereche w;
while(fin>>x>>y>>cost)
{
w.y=y;
w.cost=cost;
v[x].push_back(w);
}
init(n+1);
}
struct pereche
{
int y,cost;
};
struct comparare
{
bool operator()(pereche A,pereche B)
{
return A.y>B.y;
}
};
vector<pereche>v[N];
bitset<N>inq;
priority_queue<pereche,vector<pereche>,comparare>q;
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();
bfs();
for(int i=2;i<=n;i++)
if(dp[i]==nr)
fout<<0<<" ";
else fout<<dp[i]<<" ";
return 0;
}
}