Pagini recente » Cod sursa (job #2963358) | Cod sursa (job #1223096) | Cod sursa (job #486755) | Cod sursa (job #18353) | Cod sursa (job #2786737)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const long long infinity = 2e12;
struct Edge
{
int destination;
long long cost;
inline bool operator < (const Edge &other) const
{
return cost > other.cost;
}
};
priority_queue <Edge> Heap;
vector <Edge> v[50005];
long long distances[50005];
void Dijkstra(int n)
{
for (int i = 1; i <= n; i++)
{
distances[i] = infinity;
}
Edge firstEdge;
firstEdge.cost = 0;
firstEdge.destination = 1;
distances[1] = 0;
Heap.push(firstEdge);
while (!Heap.empty())
{
Edge currEdge = Heap.top();
Heap.pop();
for (Edge newEdge : v[currEdge.destination])
{
if (distances[newEdge.destination] <= newEdge.cost + distances[currEdge.destination])
{
continue;
}
distances[newEdge.destination] = newEdge.cost + distances[currEdge.destination];
newEdge.cost = distances[newEdge.destination];
Heap.push(newEdge);
}
}
}
int main()
{
int n, m;
ios_base::sync_with_stdio(false);
fin >> n >> m;
for (int i = 0; i < m; i++)
{
int a, b, c;
fin >> a >> b >> c;
Edge newEdge;
newEdge.cost = c;
newEdge.destination = b;
v[a].push_back(newEdge);
}
Dijkstra(n);
for (int i = 2; i <= n; i++)
{
if (distances[i] == infinity)
{
distances[i] = 0;
}
fout << distances[i] << ' ';
}
fout << '\n';
return 0;
}