Pagini recente » Cod sursa (job #1308570) | Cod sursa (job #2445845) | Cod sursa (job #1145489) | Cod sursa (job #823906) | Cod sursa (job #3125917)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int N = 5e4;
const int INF = 1 << 30;
struct succesor
{
int vf, c;
};
vector <succesor> s[N+1];
bool selectat[N+1];
int n, d[N+1], h[N+1], poz[N+1];
void dijkstra(int x0)
{
priority_queue <pair <int, int>> h;
for (int i = 1; i <= n; i++)
{
d[i] = INF;
selectat[i] = false;
}
d[x0] = 0;
h.push({0, x0});
while (!h.empty())
{
int x = h.top().second;
h.pop();
if (selectat[x])
{
continue;
}
selectat[x] = true;
for (auto p: s[x])
{
int y = p.vf;
int c = p.c;
if (selectat[y])
{
continue;
}
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");
int m;
in >> n >> m;
for (int i = 0; i < m; i++)
{
int x, y, c;
in >> x >> y >> c;
s[x].push_back((succesor){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;
}