Cod sursa(job #3379138)

Utilizator ioanaghisoiuGhiosiu Ioana-Diana ioanaghisoiu Data 29 noiembrie 2026 15:45:09
Problema Drumuri minime Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("dmin.in");
ofstream fout("dmin.out");

long long INF = 1000000000000000000LL;

int main() {
    int n, m;
    fin >> n >> m;

    vector<vector<pair<int, int>>> graph(n + 1);
    for (int i = 0; i < m; i++) {
        int x, y, c;
        fin >> x >> y >> c;
        graph[x].push_back({y, c});
        graph[y].push_back({x, c});
    }

    vector<long long> d(n + 1, INF);
    vector<int> cnt(n + 1, 0);
    vector<bool> viz(n + 1, false);

    priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;

    d[1] = 1;
    cnt[1] = 1;
    pq.push({1, 1});

    while (!pq.empty()) {
        int u = pq.top().second;
        pq.pop();

        if (viz[u]) {
            continue;
        }
        viz[u] = true;

        for (size_t i = 0; i < graph[u].size(); i++) {
            int v = graph[u][i].first;
            int cost = graph[u][i].second;
            long long new_d = d[u] * cost;

            if (new_d < d[v]) {
                d[v] = new_d;
                cnt[v] = cnt[u];
                pq.push({d[v], v});
            } else if (new_d == d[v]) {
                cnt[v] = (cnt[v] + cnt[u]) % 104659;
            }
        }
    }

    for (int i = 2; i <= n; i++) {
        fout << cnt[i] << " ";
    }
    fout << "\n";

    return 0;
}