Pagini recente » Cod sursa (job #1003951) | Cod sursa (job #774669) | Cod sursa (job #2671352) | Cod sursa (job #445397) | Cod sursa (job #2884152)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
vector <pair <int, int>> v[50001];
struct el
{
int c, x;
bool operator < (const el &alt) const
{
return c > alt.c;
}
};
int d[50001];
priority_queue <el> p;
int main()
{
int n, m, i, j, x, y, z;
el na;
fin >> n >> m;
for (i = 1; i<=m; i++)
{
fin >> x >> y >> z;
v[x].push_back ({y, z});
}
for (i = 1; i<=n; i++)
d[i] = 1<<30;
d[1] = 0;
p.push ({0, 1});
for (i = 1; i<n; i++)
{
while (p.empty() == 0 && p.top().c > d[p.top().x])
p.pop();
if (p.empty() == 1)
break;
na = p.top();
p.pop();
for (i = 0; i<v[na.x].size(); i++)
{
y = v[na.x][i].first;
z = v[na.x][i].second;
if (d[y] > na.c + z)
{
d[y] = na.c + z;
p.push ({d[y], y});
}
}
}
for (i = 2; i<=n; i++)
{
if (d[i] == (1<<30))
fout << 0 << ' ';
else
fout << d[i] << ' ';
}
return 0;
}