Pagini recente » Cod sursa (job #1735541) | Borderou de evaluare (job #1423775) | Cod sursa (job #167913) | Cod sursa (job #1276089) | Cod sursa (job #3337110)
#include<bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int inf = 2e9+7;
int n,m,i,j,x,y,c;
int d[50005];
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>>q;
vector<pair<int,int>>G[50005];
void bfs(int nod)
{
d[nod]=0;
q.push({nod,0});
while(!q.empty())
{
int j=q.top().first;
int cost=q.top().second;
q.pop();
if(d[j]==cost)
for(i=0;i<G[j].size();i++)
{
auto x=G[j][i];
if(d[x.first]>d[j]+x.second)
{
d[x.first]=d[j]+x.second;
q.push({x.first,d[x.first]});
}
}
}
}
int main()
{
fin>>n>>m;
for(i=1;i<=m;i++)
{
fin>>x>>y>>c;
G[x].push_back({y,c});
}
for(i=1;i<=n;i++)
{
d[i]=inf;
}
bfs(1);
for(i=2;i<=n;i++)
{
if(d[i]==inf)
d[i]=0;
fout<<d[i]<<" ";
}
return 0;
}