Cod sursa(job #3344503)

Utilizator drsbosDarius Scripcaru drsbos Data 2 martie 2026 10:35:23
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb
#include <iostream>
#include <fstream>
#include <stack>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <cstring>
#include <map>
#include <string>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define oo 2000000000
#define MOD 123457
using namespace std;

ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
vector<pair<int, int>>l[50005];//nod,cost
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>pq;//cost,nod
int n, m,dist[50005];
int x, y,c;

void dij(int start)
{
	for (int i = 1; i <= n; i++)
		dist[i] = 2e9;
	dist[start] = 0;
	pq.push({ 0,start });
	while (!pq.empty())
	{
		int currnod = pq.top().second;
		int currcost= pq.top().first;
		pq.pop();
		if (currcost > dist[currnod])continue;
		for (auto next : l[currnod])
		{
			if (dist[next.first] > currcost + next.second)
			{
				dist[next.first] = currcost + next.second;
				pq.push({ dist[next.first] ,next.first });
			}
		}
	}
}
int main()
{
	fin >> n >> m;
	for (int i = 1; i <= m; i++)
	{
		fin >> x >> y >> c;
		l[x].push_back({ y,c });
	}
	dij(1);
	for (int i = 2; i <= n; i++)
		if (dist[i] == oo)
			fout << "0 ";
		else
		fout << dist[i] << " ";

}