Cod sursa(job #3286963)

Utilizator drsbosDarius Scripcaru drsbos Data 14 martie 2025 21:02:15
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 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");
vector<pair<int, int>>l[55000];//nod,cost;
priority_queue < pair<int, int>, vector< pair<int, int>>, greater< pair<int, int>>>pq;//cost,nod;
int n, m, x, y,dist[55000],c;

void dj()
{
	for (int i = 2; i <= n; i++)
		dist[i] = oo;
	pq.push({ 0,1 });
	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;
	while (m--)
	{
		fin >> x >> y >> c;
		l[x].push_back({ y,c });
	}
	dj();
	for (int i = 2; i <= n; i++)
		if (dist[i] == oo)
			fout << "0 ";
		else
		fout << dist[i] << " ";

}