Pagini recente » Cod sursa (job #1255382) | Cod sursa (job #1571900) | Cod sursa (job #1495678) | Cod sursa (job #398819) | Cod sursa (job #2428796)
#include <bits/stdc++.h>
#define NMax 50006
#define oo (1 << 30)
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
int n, m;
vector <pair <int, int> > L[NMax];
priority_queue <pair <int, int> > Q;
int drum[NMax];
void Read ()
{
int x, y, cost;
fin >> n >> m;
for (int i = 1 ; i <= m; i++)
{
fin >> x >> y >> cost;
L[x].push_back ({y, cost});
}
}
void Initilizare ()
{
for (int i = 1; i <= n; i++)
drum[i] = oo;
}
void Dijkstra ()
{
int i, nod, cost;
Q.push ({0, 1});
Initilizare();
drum[1] = 0;
while (!Q.empty())
{
i = Q.top().second;
Q.pop();
for (int j = 0; j < L[i].size(); j++)
{
nod = L[i][j].first;
cost = L[i][j].second;
if (drum[nod] > drum[i] + cost)
{
drum[nod] = drum[i] + cost;
Q.push ({-drum[nod], nod});
}
}
}
for (int i = 2; i <= n; i++)
fout << drum[i] << " ";
}
int main()
{
Read();
Dijkstra();
return 0;
}