Cod sursa(job #3380662)

Utilizator maria_5Diaconescu Maria maria_5 Data 2 decembrie 2026 08:52:55
Problema Drumuri minime Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.77 kb

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <utility>

using namespace std;

const int N_MAX = 1505;
const int MOD = 104659;
const double INF = 1e18;
const double EPS = 1e-9;

vector<pair<int, double>> adj[N_MAX];
double dist[N_MAX];
int nr[N_MAX];

void dijkstra(int start, int N) {
    for (int i = 1; i <= N; i++) {
        dist[i] = INF;
        nr[i] = 0;
    }

    priority_queue<pair<double, int>, vector<pair<double, int>>, greater<pair<double, int>>> pq;
    dist[start] = 0.0;
    nr[start] = 1;
    pq.push({0.0, start});

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

        if (d > dist[u] + EPS) {
            continue;
        }

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

            if (dist[u] + cost < dist[v] - EPS) {
                dist[v] = dist[u] + cost;
                nr[v] = nr[u];
                pq.push({dist[v], v});
            } else if (fabs((dist[u] + cost) - dist[v]) <= EPS) {
                nr[v] = (nr[v] + nr[u]) % MOD;
            }
        }
    }
}

int main() {
    freopen("dmin.in", "r", stdin);
    freopen("dmin.out", "w", stdout);

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int N, M;
    if (!(cin >> N >> M)) return 0;

    for (int i = 0; i < M; i++) {
        int x, y;
        long long c;
        cin >> x >> y >> c;
        double w = log((double)c);
        adj[x].push_back({y, w});
        adj[y].push_back({x, w});
    }

    dijkstra(1, N);

    for (int i = 2; i <= N; i++) {
        cout << nr[i] << " ";
    }
    cout << "\n";

    return 0;
}