Pagini recente » Cod sursa (job #2484189) | Cod sursa (job #386400) | Cod sursa (job #359119) | Cod sursa (job #1500219) | Cod sursa (job #3286705)
#include <bits/stdc++.h>
using namespace std;
vector <pair<int,int>> G[50005];
int n,m;
int d[50005];
bool sel[50005];
void dijkstra(int nod)
{
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> q;
for(int i=2; i<=n; ++i)
{
d[i]=INT_MAX;
}
for(auto it: G[nod])
{
d[it.second]=it.first;
q.push(it);
}
sel[nod]=true;
while(!q.empty())
{
while(!q.empty() && sel[q.top().second])q.pop();
if(!q.empty())
{
int k=q.top().second;
sel[k]=true;
q.pop();
for(auto it: G[k])
{
if(d[it.second]>d[k]+it.first)
{
d[it.second]=d[k]+it.first;
q.push({d[it.second],it.second});
}
}
}
}
}
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
cin.sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=1; i<=m; ++i)
{
int x,y,z;
cin>>x>>y>>z;
G[x].push_back({z,y});
}
dijkstra(1);
for(int i=2; i<=n; ++i)
{
if(d[i]!=INT_MAX)cout<<d[i]<<' ';
else cout<<0<<' ';
}
return 0;
}