Cod sursa(job #3378066)

Utilizator carinaa_leetAmarie Carina carinaa_leet Data 25 noiembrie 2026 10:59:24
Problema Drumuri minime Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.85 kb
// Dijkstra cu logaritmare
#include <fstream>
#include <queue>
#include <vector>
#include <cmath>

using namespace std;

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

const int MOD = 104659;
const double INF = 1e18;
const double EPS = 1e-10;

int n, m;
vector<vector<pair<int, double>>> adj;
vector<double> d;
vector<int> cnt;
priority_queue<pair<double, int>, vector<pair<double, int>>, greater<pair<double, int>>> pq;

int main()
{
    fin >> n >> m;
    adj.resize(n + 1);
    d.resize(n + 1, INF);
    cnt.resize(n + 1, 0);

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

        // transformam costul prin logaritmare
        double cost_log = log(c);

        adj[x].push_back({y, cost_log});
        adj[y].push_back({x, cost_log});
    }

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

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

        // ignoram starile vechi din coada
        if (dist_curenta > d[u] + EPS)
            continue;

        for (auto &muchie : adj[u])
        {
            int vecin = muchie.first;
            double cost = muchie.second;

            // caz 1: am gasit un drum strict mai scurt
            if (d[u] + cost < d[vecin] - EPS)
            {
                d[vecin] = d[u] + cost;
                cnt[vecin] = cnt[u];
                pq.push({d[vecin], vecin});
            }
            // caz 2: am gasit un drum de aceeasi lungime minima
            else if (abs(d[u] + cost - d[vecin]) <= EPS)
            {
                cnt[vecin] = (cnt[vecin] + cnt[u]) % MOD;
            }
        }
    }

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

    return 0;
}