Cod sursa(job #3283952)

Utilizator drsbosDarius Scripcaru drsbos Data 10 martie 2025 19:04:07
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <fstream>
#include <stack>
#include <queue>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <set>
#include <cstring>
#include <map>
#include <string>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define oo 2000000
#define MOD 1000000007
using namespace std;

ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, x, y, c;
priority_queue < pair<int, int>, vector< pair<int, int> >, greater<pair<int, int> > >pq;
vector< pair<int, int>>l[50005];
int dist[50005];
void dj()
{
	for (int i = 1; i <= n; i++)
		dist[i] = 2e9;
	dist[1] = 0;
	pq.push({ 0,1 });
	while (!pq.empty())
	{
		int currcost = pq.top().first;
		int currnod = pq.top().second;
		pq.pop();
		if (currcost > dist[currnod])continue;
		for (auto next : l[currnod])
		{
			if (dist[next.first] > next.second + currcost)
			{
				dist[next.first] = next.second + currcost;
				pq.push({ dist[next.first],next.first });
			}
		}
	}
}
int main()
{
	fin >> n >> m;
	while(m--)
	{
		fin >> x >> y >> c;
		l[x].push_back({y,c});
	}
	dj();
	for (int i = 2; i <= n; i++)
		if (dist[i] == 2e9)
			fout << "0 ";
		else
			fout << dist[i] << " ";

}