Pagini recente » Cod sursa (job #1358763) | Cod sursa (job #520785) | Cod sursa (job #1972861) | Cod sursa (job #439644) | Cod sursa (job #3216216)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
vector <pair<int,int>> v[50005];
int n,m;
void citire()
{
fin>>n>>m;
for(int i=1;i<=m;i++)
{
int x,y,cost;
fin>>x>>y>>cost;
v[x].push_back({y,cost});
}
}
int viz[50005],d[50005];
void dijkstra(int k)
{
for(int i=1;i<=n;i++)
{
viz[i]=0;
d[i]=INT_MAX;
}
priority_queue<pair<int,int>>q;
q.push({0,k});
d[k]=0;
while(!q.empty())
{
int x=q.top().second;
q.pop();
if(viz[x])
continue;
viz[x]=1;
for(auto i:v[x])
{
int cost;
cost=d[x]+i.second;
if(d[i.first]>cost){
d[i.first]=cost;
if(!viz[i.first])
{
q.push({-d[i.first],i.first});
}
}
}
}
for(int i=2;i<=n;i++)
if(d[i]==INT_MAX)
fout<<"0 ";
else fout<<d[i]<<" ";
}
int main()
{
citire();
dijkstra(1);
return 0;
}