Cod sursa(job #3380677)

Utilizator GabrielaTudoracheTudorache Gabriela GabrielaTudorache Data 2 decembrie 2026 09:10:28
Problema Drumuri minime Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <bits/stdc++.h>
using namespace std;

const int MOD = 104659;
const long double EPS = 1e-12L;

int main() {
    FILE* fin = fopen("dmin.in", "r");
    FILE* fout = fopen("dmin.out", "w");
    int n, m;
    fscanf(fin, "%d %d", &n, &m);
    vector<vector<pair<int, long double>>> adj(n + 1);
    for (int i = 0; i < m; i++) {
        int x, y;
        long long c;
        fscanf(fin, "%d %d %lld", &x, &y, &c);
        long double w = logl((long double)c);
        adj[x].push_back({y, w});
        adj[y].push_back({x, w});
    }
    vector<long double> dist(n + 1, 1e18L);   
    vector<int> cnt(n + 1, 0);                
    typedef pair<long double, int> State;
    priority_queue<State, vector<State>, greater<State>> pq;
    dist[1] = 0;
    cnt[1] = 1;
    pq.push({0, 1});
    while (!pq.empty()) {
        State top = pq.top();
        pq.pop();
        long double d = top.first;
        int u = top.second;
        if (d > dist[u] + EPS) continue;  
        for (auto& e : adj[u]) {
            int v = e.first;
            long double nd = dist[u] + e.second;
            if (nd < dist[v] - EPS) {          
                dist[v] = nd;
                cnt[v] = cnt[u];
                pq.push({nd, v});
            } else if (fabsl(nd - dist[v]) <= EPS) {  
                cnt[v] = (cnt[v] + cnt[u]) % MOD;
            }
        }
    }
    for (int i = 2; i <= n; i++) fprintf(fout, "%d%c", cnt[i], i == n ? '\n' : ' ');
    if (n == 1) fprintf(fout, "\n");
    return 0;
}