Pagini recente » Cod sursa (job #1861308) | Cod sursa (job #1689203) | Cod sursa (job #2554348) | Cod sursa (job #933824) | Cod sursa (job #2379102)
#include <fstream>
#include <queue>
#include <vector>
#include <functional>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n, m, a, b, c;
vector<vector<pair<int, int>>> edges;
vector<int> cost;
priority_queue<int, vector<int>, function<bool(int, int)>> pq([](int a, int b) { return cost[a] > cost[b]; });
int main()
{
f >> n >> m;
edges.resize(n + 1);
cost.resize(n + 1, 1e9);
while (m--)
f >> a >> b >> c,
edges[a].push_back(make_pair(b, c));
pq.push(1);
cost[1] = 0;
while (!pq.empty())
{
int x = pq.top();
pq.pop();
for (auto const& p : edges[x])
if (cost[p.first] > cost[x] + p.second)
cost[p.first] = cost[x] + p.second,
pq.push(p.first);
}
for (int i = 2; i <= n; ++i)
g << ((cost[i] < 1e9) ? cost[i] : 0) << ' ';
return 0;
}