Cod sursa(job #3380066)

Utilizator DartokeMoisi David Dartoke Data 1 decembrie 2026 19:49:29
Problema Drumuri minime Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <fstream>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;

const int MOD = 104659;
const double EPS = 1e-9;

int main() {
    ios::sync_with_stdio(false);
    ifstream fin("dmin.in");
    ofstream fout("dmin.out");

    int n, m;
    fin >> n >> m;
    vector<vector<pair<int, double>>> g(n + 1);
    for (int i = 0; i < m; i++) {
        int x, y;
        long long c;
        fin >> x >> y >> c;
        double lc = std::log((double)c);
        g[x].push_back({y, lc});
        g[y].push_back({x, lc});
    }

    vector<double> dist(n + 1, 1e18);
    vector<int> cnt(n + 1, 0);
    vector<bool> done(n + 1, false);
    priority_queue<pair<double, int>, vector<pair<double, int>>, greater<pair<double, int>>> pq;

    dist[1] = 0;
    cnt[1] = 1;
    pq.push({0.0, 1});
    while (!pq.empty()) {
        int u = pq.top().second;
        pq.pop();
        if (done[u]) continue;
        done[u] = true;                 
        for (auto& [v, w] : g[u]) {
            if (done[v]) continue;
            double nd = dist[u] + w;
            if (nd < dist[v] - EPS) { 
                dist[v] = nd;
                cnt[v] = cnt[u];
                pq.push({nd, v});
            } else if (nd < dist[v] + EPS) { 
                cnt[v] = (cnt[v] + cnt[u]) % MOD;
            }
        }
    }
    for (int i = 2; i <= n; i++) fout << cnt[i] << (i == n ? '\n' : ' ');
    return 0;
}