Pagini recente » Cod sursa (job #2211562) | Cod sursa (job #2067038) | Cod sursa (job #1376130) | Cod sursa (job #651815) | Cod sursa (job #2869605)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
struct muchie
{
int nod, cost;
bool operator < (const muchie A)const
{
return cost > A.cost;
}
};
int n, m;
long long d[50003];
vector<muchie> h[50003];
bitset<50003> viz;
void Citire()
{
int x, y, c;
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
fin >> x >> y >> c;
h[x].push_back({y, c});
}
}
void Dijkstra()
{
int nod;
for (int i = 1; i <= n; i++)
d[i] = 1e9;
priority_queue<muchie> q;
q.push({1, 0});
d[1] = 0;
while (!q.empty())
{
nod = q.top().nod;
q.pop();
if (viz[nod] == 0)
{
viz[nod] = 1;
for (auto w : h[nod])
{
if (d[w.nod] > d[nod] + w.cost)
{
d[w.nod] = d[nod] + w.cost;
q.push({w.nod, d[w.nod]});
}
}
}
}
for (int i = 2; i <= n; i++)
if (d[i] != 1e9) fout << d[i] << " ";
else fout << "0 ";
}
int main()
{
Citire();
Dijkstra();
return 0;
}