Pagini recente » Cod sursa (job #1125803) | Cod sursa (job #1059735) | Cod sursa (job #1820240) | Cod sursa (job #49442) | Cod sursa (job #3155794)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream in ("dijkstra.in");
ofstream out ("dijkstra.out");
struct str{
int nod, cost;
bool operator < (const str & aux) const
{
return cost > aux.cost;
}
};
const int max_size = 5e4 + 1, INF = 2e9 + 1;
int d[max_size];
vector <pair <int, int>> mc[max_size];
priority_queue <str> pq;
void djk ()
{
pq.push({1, 0});
d[1] = 0;
while (!pq.empty())
{
int nod = pq.top().nod, val = pq.top().cost;
pq.pop();
if (val > d[nod])
{
continue;
}
for (auto f : mc[nod])
{
if (d[nod] + f.second < d[f.first])
{
d[f.first] = d[nod] + f.second;
pq.push({f.first, d[f.first]});
}
}
}
}
int main ()
{
int n, m;
in >> n >> m;
while (m--)
{
int x, y, c;
in >> x >> y >> c;
mc[x].push_back({y, c});
}
for (int i = 1; i <= n; i++)
{
d[i] = INF;
}
djk();
for (int i = 2; i <= n; i++)
{
if (d[i] == INF)
{
d[i] = 0;
}
out << d[i] << " ";
}
in.close();
out.close();
return 0;
}