Cod sursa(job #3380253)

Utilizator mateivoican2006@gmail.comVoican Matei [email protected] Data 1 decembrie 2026 22:05:38
Problema Drumuri minime Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <utility>
#include <functional>
using namespace std;

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

int main() {
    FILE *in = fopen("dmin.in", "r");
    int n, m;
    fscanf(in, "%d %d", &n, &m);

    vector<vector<pair<int, double>>> graph(n + 1);
    for (int i = 0; i < m; i++) {
        int x, y, cost;
        fscanf(in, "%d %d %d", &x, &y, &cost);
        double weight = log((double)cost);
        graph[x].push_back({y, weight});
        graph[y].push_back({x, weight});
    }
    fclose(in);

    vector<double> dist(n + 1, 1e18);
    vector<int> ways(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;
    ways[1] = 1;
    pq.push({0, 1});

    while (!pq.empty()) {
        int node = pq.top().second;
        pq.pop();
        if (done[node]) continue;
        done[node] = true;
        for (auto &edge : graph[node]) {
            int next = edge.first;
            double candidate = dist[node] + edge.second;
            if (candidate < dist[next] - EPS) {
                dist[next] = candidate;
                ways[next] = ways[node];
                pq.push({candidate, next});
            } else if (fabs(candidate - dist[next]) <= EPS) {
                ways[next] = (ways[next] + ways[node]) % MOD;
            }
        }
    }

    FILE *out = fopen("dmin.out", "w");
    for (int i = 2; i <= n; i++) fprintf(out, i == 2 ? "%d" : " %d", ways[i]);
    fprintf(out, "\n");
    fclose(out);
    return 0;
}