Pagini recente » Cod sursa (job #750149) | Cod sursa (job #1828181) | Cod sursa (job #395714) | Cod sursa (job #1715431) | Cod sursa (job #2843516)
#include <fstream>
#include <queue>
#include <vector>
#include <bitset>
using namespace std;
const int N = 50001;
const int INF = 1e9 + 1;
vector <pair <int, int> > succesori[N];
bitset <N> selectat;
priority_queue <pair <int, int> > h;
int d[N], n, m;
void dijkstra(int x0)
{
for (int i = 1; i <= n; i++)
{
d[i] = INF;
}
d[x0] = 0;
h.push({0, x0});
while (!h.empty())
{
int x = h.top().second;
h.pop();
if (selectat[x]) continue;
selectat[x] = 1;
for (auto p: succesori[x])
{
int y = p.first;
int c = p.second;
if (d[x] + c < d[y])
{
d[y] = d[x] + c;
h.push({-d[y], y});
}
}
}
}
int main()
{
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
in >> n >> m;
for (int i = 0; i < m; i++)
{
int x, y, c;
in >> x >> y >> c;
succesori[x].push_back({y, c});
}
dijkstra(1);
for (int i = 2; i <= n; i++)
{
if (d[i] == INF)
{
d[i] = 0;
}
out << d[i] << " ";
}
in.close();
out.close();
return 0;
}