Pagini recente » Cod sursa (job #1059821) | Cod sursa (job #1109444) | Cod sursa (job #748331) | Cod sursa (job #2228762) | Cod sursa (job #2871131)
#include <iostream>
#include <queue>
#include <fstream>
#include <bitset>
#include <vector>
using namespace std;
const int N=50005,nr=141554145;
int dp[N],n,m;
ifstream fin ("dijkstra.in");
ofstream fout("dijkstra.out");
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(){
pereche w;
fin>>n>>m;
int x,y,cost;
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()
{
pereche x;
inq[1]=true;
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;
}
else fout<<dp[i]<<" ";
}