Cod sursa(job #3380600)

Utilizator TLuca2021Teodorescu Luca Nicolae TLuca2021 Data 2 decembrie 2026 06:28:38
Problema Drumuri minime Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.76 kb
#include <cmath>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("dmin.in");
ofstream fout("dmin.out");
const double MAX_VAL = 1e18;
const double EPS = 1e-10;
const int MOD = 104659;
struct Edge {
    int to;
    double weight;
};
struct State {
    double dist;
    int node;
    bool operator>(const State& other) const {
        return dist > other.dist;
    }
};
void solve() {
    int nodes, edges;
    fin >> nodes >> edges;
    vector<vector<Edge>> graph(nodes + 1);
    for (int i = 0; i < edges; i++) {
        int u, v;
        double raw_weight;
        fin >> u >> v >> raw_weight;
        double log_weight = log(raw_weight);
        graph[u].push_back({v, log_weight});
        graph[v].push_back({u, log_weight});
    }
    vector<double> dist(nodes + 1, MAX_VAL);
    vector<int> ways(nodes + 1, 0);
    priority_queue<State, vector<State>, greater<State>> heap;
    dist[1] = 0.0;
    ways[1] = 1;
    heap.push({0.0, 1});
    while (!heap.empty()) {
        State curr = heap.top();
        heap.pop();
        if (curr.dist > dist[curr.node] + EPS) {
            continue;
        }
        for (const Edge& edge : graph[curr.node]) {
            int next_node = edge.to;
            double next_dist = curr.dist + edge.weight;
            if (next_dist + EPS < dist[next_node]) {
                dist[next_node] = next_dist;
                ways[next_node] = ways[curr.node];
                heap.push({next_dist, next_node});
            } else if (abs(next_dist - dist[next_node]) <= EPS) {
                ways[next_node] = (ways[next_node] + ways[curr.node]) % MOD;
            }
        }
    }
    for (int i = 2; i <= nodes; i++) {
        fout << ways[i] << " ";
    }
    fout << "\n";
}
int main() {
    solve();
    return 0;
}