Cod sursa(job #3377069)

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

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<int> costuri;
		priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, 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())
		{
			int cost, 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] + costVecin)
				{
					costuri[vecin] = costuri[nod] + costVecin;
					cnt[vecin] = cnt[nod];
					coada.push(pair(costuri[vecin], vecin));
				}
				else if (costuri[vecin] == costuri[nod] + costVecin)
				{
					cnt[vecin] += cnt[nod];
				}
			}
		}

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

	return 0;
}