Pagini recente » Cod sursa (job #1220484) | Cod sursa (job #1678482) | Cod sursa (job #384023) | Cod sursa (job #3356947) | Cod sursa (job #3323852)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, dp[50552];
const int INF = 1e9;
vector<pair<int, int>> a[50002];
void Djk(int start)
{
priority_queue<pair<int, int>> q;
for (int i = 1; i <= n; i++)
dp[i] = INF;
dp[start] = 0;
q.push({0, start});
while (!q.empty())
{
int nod = q.top().second;
q.pop();
for (auto w : a[nod])
{
if (dp[nod] + w.second < dp[w.first])
{
dp[w.first] = dp[nod] + w.second;
q.push({-w.second, w.first});
}
}
}
}
int main()
{
fin.tie(0);
std::ios_base::sync_with_stdio(false);
fin >> n >> m;
int x, y, c;
for (int i = 1; i <= m; i++)
{
fin >> x >> y >> c;
a[x].push_back({y, c});
// a[y].push_back({x, c});
}
Djk(1);
for (int i = 2; i <= n; i++)
if (dp[i] != INF)
fout << dp[i] << ' ';
else
fout << "0 ";
fout << '\n';
return 0;
}