Pagini recente » Cod sursa (job #371082) | Cod sursa (job #2805031) | Cod sursa (job #2119090) | Cod sursa (job #2281261) | Cod sursa (job #2376292)
#include <iostream>
#include <fstream>
#include <vector>
#include <climits>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n,m;
vector <pair <int,int> >G[50010];
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > Q;
int dp[50010],ok[50010];
void citire()
{
fin>>n>>m;
int x,y,c;
for(int i=1; i<=m; i++)
{
fin>>x>>y>>c;
G[x].push_back({y,c});
}
for(int i=2; i<=n; i++)
dp[i]=INT_MAX/2;
}
void dijkstra()
{
ok[1]=1;
Q.push({dp[1],1});
while(!Q.empty())
{
int x=Q.top().second;
ok[x]=0;
Q.pop();
for(auto v:G[x])
{ int cost=v.second;
int y=v.first;
if(dp[y]>dp[x]+cost)
{
dp[y]=dp[x]+cost;
if(!ok[y])
{
ok[y]=1;
Q.push({dp[y],y});
}
}
}
}
}
int main()
{
citire();
dijkstra();
for(int i=2; i<=n; i++)
if(dp[i]!=INT_MAX/2)fout<<dp[i]<<" ";
else
fout<<0<<" ";
return 0;
}