Cod sursa(job #3378378)

Utilizator RaducuSebasRaducu Sebastian RaducuSebas Data 26 noiembrie 2026 09:05:28
Problema Drumuri minime Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dmin.in");
ofstream fout("dmin.out");

struct muchie {
	int x, c;
};
const int N = 1502;
const int MOD = 104659;
const int BAZA = 1000000000;

vector<muchie> g[N];
vector<int> dist[N];
int nr[N], viz[N], n, m;

vector<int> produs(const vector<int> &a, int b) {

	vector<int> rez;
	long long transport = 0;
	for (int x : a) {

		long long p = 1LL * x * b + transport;
		rez.push_back(p % BAZA);
		transport = p / BAZA;
	}
	if (transport) rez.push_back(transport);
	return rez;
}

int compara(const vector<int> &a, const vector<int> &b) {

	if (a.size() != b.size()) return a.size() < b.size() ? -1 : 1;
	for (int i = (int)a.size() - 1; i >= 0; i--)
		if (a[i] != b[i]) return a[i] < b[i] ? -1 : 1;
	return 0;
}

struct stare {
	int x;
	vector<int> c;
	bool operator<(const stare &other) const { return compara(c, other.c) > 0; }
};

void dijkstra() {

	priority_queue<stare> H;
	dist[1] = {1};
	nr[1] = 1;
	H.push({1, dist[1]});
	while (!H.empty()) {

		int nod = H.top().x;
		H.pop();
		if (viz[nod]) continue;
		viz[nod] = 1;

		for (muchie vecin : g[nod]) {

			int x = vecin.x;
			if (viz[x]) continue;
			vector<int> cost = produs(dist[nod], vecin.c);
			int cmp = dist[x].empty() ? -1 : compara(cost, dist[x]);
			if (cmp < 0) {

				dist[x] = cost;
				nr[x] = nr[nod];
				H.push({x, dist[x]});
			} else if (cmp == 0) nr[x] = (nr[x] + nr[nod]) % MOD;
		}
	}
}

int main() {

	int x, y, c;
	fin >> n >> m;
	for (int i = 1; i <= m; i++) {

		fin >> x >> y >> c;
		g[x].push_back({y, c});
		g[y].push_back({x, c});
	}

	dijkstra();
	for (int i = 2; i <= n; i++) {

		if (i > 2) fout << ' ';
		fout << nr[i];
	}
	fout << '\n';
}