Pagini recente » Cod sursa (job #1603394) | Cod sursa (job #2174747) | Cod sursa (job #2106679) | Borderou de evaluare (job #2116710) | Cod sursa (job #2415286)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMAX = 50000;
const int INF = 1e9 + 5;
struct NodeDist
{
int node, dist;
bool operator < (const NodeDist other) const
{
return dist > other.dist;
}
};
int N, M;
vector <NodeDist> g[NMAX + 5];
int dist[NMAX + 5];
priority_queue <NodeDist> pq;
int main()
{
fin >> N >> M;
int x, y, z;
for(int i = 1; i <= M; i++)
{
fin >> x >> y >> z;
g[x].push_back({y, z});
}
for(int i = 1; i <= N; i++)
dist[i] = INF;
pq.push({1, 0});
while(!pq.empty())
{
int currentNode = pq.top().node;
int currentDist = pq.top().dist;
pq.pop();
if(dist[currentNode] != INF)
continue;
dist[currentNode] = currentDist;
for(auto it : g[currentNode])
if(dist[it.node] == INF)
pq.push({it.node, currentDist + it.dist});
}
for(int i = 2; i <= N; i++)
fout << dist[i] << ' ';
return 0;
}