Pagini recente » Cod sursa (job #2407903) | Cod sursa (job #1762179) | Cod sursa (job #1874022) | Cod sursa (job #95287) | Cod sursa (job #2206896)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define NMAX 50002
#define INF 0x3f3f3f3f
using namespace std;
ifstream f("dijkstra.in");
ofstream o("dijkstra.out");
int n,m;
vector <pair<int,int>> g[NMAX];
int dist[NMAX];
priority_queue <pair<int,int>> q;
void citire()
{
f >> n >> m;
int x,y,c;
for(int i = 1; i <= m; ++i)
{
f >> x >> y >> c;
g[x].push_back({y,c});
}
}
void dijkstra()
{
for(int i = 2; i <= n; ++i)
dist[i] = INF;
q.push({0,1});
while(not q.empty())
{
int nod = q.top().second;
int d = - q.top().first;
q.pop();
if(dist[nod] != d)
continue;
for(auto i: g[nod])
if(dist[i.first] > dist[nod] + i.second)
{
dist[i.first] = dist[nod] + i.second;
q.push({-dist[i.first],i.first});
}
}
}
void afis()
{
for(int i = 2; i <= n; ++i)
if(dist[i] == INF)
o << "0 ";
else
o << dist[i] << ' ';
}
int main()
{
citire();
dijkstra();
afis();
return 0;
}