Pagini recente » Cod sursa (job #2930172) | Cod sursa (job #636296) | Cod sursa (job #1870534) | Cod sursa (job #1681215) | Cod sursa (job #2574509)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int nmax = 50005;
const int inf = (1 << 30);
int n, m;
int dist[nmax];
struct comp
{
bool operator() (int a, int b)
{
return dist[a] > dist[b];
}
};
struct Point
{
int t, c;
};
priority_queue < int, vector < int >, comp > q;
vector < Point > l[nmax];
bitset < nmax > viz;
void Dijkstra()
{
q.push(1);
viz[1] = 1;
while (!q.empty())
{
int nod = q.top();
q.pop();
viz[nod] = 0;
for (Point el : l[nod])
{
if (dist[nod] + el.c < dist[el.t])
{
dist[el.t] = dist[nod] + el.c;
if (!viz[el.t])
{
viz[el.t] = 1;
q.push(el.t);
}
}
}
}
}
int main()
{
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y, c;
fin >> x >> y >> c;
l[x].push_back({y, c});
}
for (int i = 2; i <= n; i++)
dist[i] = inf;
Dijkstra();
for (int i = 2; i <= n; i++)
{
if (dist[i] == inf)
dist[i] = 0;
fout << dist[i] << ' ';
}
fin.close();
fout.close();
return 0;
}