Pagini recente » Cod sursa (job #2363606) | Cod sursa (job #577270) | Cod sursa (job #710679) | Cod sursa (job #316225) | Cod sursa (job #2329348)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
#define limn 50010
#define inf 2e9
int n,m;
vector <pair<int, int>> G[limn];
priority_queue <pair<int,int>> pq;
int viz[limn], path[limn];
int main()
{
int x,y,c,nod,cost;
fin>>n>>m;
while(m--)
{
fin>>x>>y>>c;
G[x].push_back({y,c});
}
for(int i=1; i<=n; i++) path[i] = inf;
path[1] = 0;
pq.push({0, 1});
while (!pq.empty())
{
nod = pq.top().second;
pq.pop();
if (viz[nod]) continue;
viz[nod] = 1;
for (auto it:G[nod])
if (!viz[it.first] && path[it.first] > path[nod] + it.second)
{
path[it.first] = path[nod] + it.second;
pq.push({-path[it.first], it.first});
}
}
for (int i=2; i<=n; i++)
if (path[i] != inf) fout<<path[i]<<' ';
else fout<<0<<' ';
fin.close();
fout.close();
return 0;
}