Cod sursa(job #3378169)

Utilizator robertbaciuBaciu Vlad-Robert robertbaciu Data 25 noiembrie 2026 13:42:07
Problema Drumuri minime Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.76 kb
#include <bitset>
#include <cmath>
#include <fstream>
#include <queue>
#include <vector>

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

int const NMAX = 1501;
int const MOD = 104659;
double const EPSILON = 1e-10;

std::vector<std::vector<std::pair<int, double>>> graph;
std::vector<double> dist;
std::bitset<NMAX> visited;
std::vector<int> paths;

void dijkstra(int start) {
    std::priority_queue<std::pair<double, int>,
                        std::vector<std::pair<double, int>>,
                        std::greater<std::pair<double, int>>>
        q;

    dist[start] = 0;
    paths[start] = 1;
    q.push({0, start});

    while (!q.empty()) {
        auto [curr_dist, curr] = q.top();
        q.pop();

        if (visited[curr]) {
            continue;
        }
        visited[curr] = 1;

        for (auto &[neighbor, weight] : graph[curr]) {
            if (curr_dist + weight + EPSILON < dist[neighbor]) {
                dist[neighbor] = curr_dist + weight;
                paths[neighbor] = paths[curr];
                q.push({dist[neighbor], neighbor});
            } else if (std::abs(curr_dist + weight - dist[neighbor]) <
                       EPSILON) {
                // curr_dist + edge_weight == dist[neighbor]
                paths[neighbor] += paths[curr];
                paths[neighbor] %= MOD;
            }
        }
    }
}

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

    graph.resize(n + 1);
    dist.assign(n + 1, 0x3f3f3f3f);
    paths.resize(n + 1);

    for (int i = 0; i < m; i++) {
        int x, y, c;
        fin >> x >> y >> c;
        graph[x].push_back({y, std::log(c)});
        graph[y].push_back({x, std::log(c)});
    }

    dijkstra(1);

    for (int i = 2; i <= n; i++) {
        fout << paths[i] << ' ';
    }

    return 0;
}