Cod sursa(job #3377074)

Utilizator Alex112358Glavan Alexandru Alex112358 Data 21 noiembrie 2026 13:51:32
Problema Drumuri minime Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>

using namespace std;

int N, M;
int cnt[1500];

ifstream f("dmin.in");
ofstream g("dmin.out");

int main() {

		f >> N >> M;

		vector<vector<pair<int, int>>> muchii;
		vector<double> costuri;
		priority_queue<pair<double, int>, vector<pair<double, int>>, greater<pair<double, int>>> coada;
		
		

		muchii.resize(N + 1);
		costuri.assign(N + 1, 1000000001);

		for (int i = 1; i <= M; i++)
		{
			int a, b, c;
			f >> a >> b >> c;
			muchii[a].push_back(pair(b, c));
			muchii[b].push_back(pair(a, c));
		}

		for (int i = 1; i <= N; i++)
			if (i == 1)
				costuri[i] = 0;
			else
				costuri[i] = 1000000001;

		cnt[1] = 1;

		coada.push(pair(0, 1));
		while (!coada.empty())
		{
			double cost; 
			int nod;
			cost = coada.top().first;
			nod = coada.top().second;
			coada.pop();

			if (cost > costuri[nod])
				continue;

			for (auto [vecin, costVecin] : muchii[nod])
			{
				if (costuri[vecin] > costuri[nod] + log(costVecin) + 1e-9)
				{
					costuri[vecin] = costuri[nod] + log(costVecin);
					cnt[vecin] = cnt[nod];
					coada.push(pair(costuri[vecin], vecin));
				}
				else if (fabs(costuri[vecin] - (costuri[nod] + log(costVecin))) < 1e-9)
				{
					cnt[vecin] = (cnt[vecin] + cnt[nod]) % 104659;
				}
			}
		}

		
		for (int i = 2; i <= N; i++)
		{
			g << cnt[i] << " ";
		}

	return 0;
}