Pagini recente » Cod sursa (job #2699678) | Cod sursa (job #1476030) | Cod sursa (job #507802) | Cod sursa (job #1326960) | Cod sursa (job #2376282)
#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>>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;
if(dp[v.first]>dp[x]+cost)
{
dp[v.first]=dp[x]+cost;
if(!ok[v.first])
{
ok[v.first]=1;
Q.push({dp[v.first],v.first});
}
}
}
}
}
int main()
{
citire();
dijkstra();
for(int i=2; i<=n; i++)
fout<<dp[i]<<" ";
return 0;
}