Pagini recente » Cod sursa (job #1832982) | Cod sursa (job #1084750) | Cod sursa (job #634323) | Cod sursa (job #1494213) | Cod sursa (job #3321337)
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
#define NMAX 50005
#define pp pair<int, int>
const int oo = (1 << 31) - 1;
vector<pp> g[NMAX];
vector<int> dist;
int n, m;
void solve()
{
priority_queue<pp, vector<pp>, greater<pp>> q;
q.push({0, 1});
dist.resize(n + 1, oo);
while (!q.empty())
{
auto x = q.top();
q.pop();
if (dist[x.second] == oo)
{
dist[x.second] = x.first;
for (auto i : g[x.second])
{
if (dist[i.first] == oo)
{
q.push({x.first + i.second, i.first});
}
}
}
}
}
int main()
{
fin >> n >> m;
for (int i = 0; i < m; i++)
{
int a, b, c;
fin >> a >> b >> c;
g[a].push_back({b, c});
}
solve();
for (int i = 2; i <= n; i++)
{
fout << dist[i] << " ";
}
return 0;
}