Pagini recente » Cod sursa (job #249351) | Cod sursa (job #1099026) | Cod sursa (job #2376312) | Cod sursa (job #1125311) | Cod sursa (job #2109961)
#include <iostream>
#include <fstream>
#include <queue>
#define Edge pair <int, int>
#define cost first
#define y second
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
int n, m;
const int MAXN = 50005;
const int inf = 0x3f3f3f;
int dist[MAXN];
vector <Edge> Graf[MAXN];
priority_queue <Edge, vector <Edge>, greater <Edge> > heap;
void citire()
{
in >> n >> m;
for(int i = 1; i <= n; i++)
{
int a, b, c;
in >> a >> b >> c;
Graf[a].push_back({c, b});
}
}
void dijkstra()
{
heap.push({0, 1});
while(!heap.empty())
{
Edge New = heap.top();
heap.pop();
for(Edge e : Graf[New.y])
{
int tmp = e.cost + dist[New.y];
if(tmp < dist[e.y])
{
dist[e.y] = tmp;
heap.push(e);
}
}
}
}
int main()
{
for(int i = 0; i < 50005; i++)
dist[i] = inf;
dist[1] = 0;
citire();
dijkstra();
for(int i = 2; i <= n; i++)
out << dist[i] << " ";
return 0;
}