Cod sursa(job #3379483)

Utilizator TomikeToma Rares Tomike Data 30 noiembrie 2026 19:17:24
Problema Drumuri minime Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <fstream>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;

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

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

int n, m;
vector<vector<pair<int, double>>> g;
vector<double> d;
vector<int> cnt;
vector<bool> fixat;

int main(){
    fin >> n >> m;
    g.resize(n + 1);
    for(int i = 1; i <= m; i++){
        int x, y, c;
        fin >> x >> y >> c;
        double w = log((double)c);
        g[x].push_back({y, w});
        g[y].push_back({x, w});
    }

    d.assign(n + 1, 1e18);
    cnt.assign(n + 1, 0);
    fixat.assign(n + 1, false);

    priority_queue<pair<double,int>, vector<pair<double,int>>, greater<pair<double,int>>> pq;
    d[1] = 0;
    cnt[1] = 1;
    pq.push({0, 1});
    while(!pq.empty()){
        int x = pq.top().second;
        pq.pop();
        if(fixat[x])continue;
        fixat[x] = true;
        for(auto e : g[x]){
            int y = e.first;
            double w =e.second;
            if(d[x] + w < d[y] - EPS){
                d[y] = d[x]  +w;
                cnt[y] = cnt[x];
                pq.push({d[y], y});
            }else if(fabs(d[x] + w - d[y]) < EPS){
                cnt[y] = (cnt[y] + cnt[x]) % MOD;
            }
        }
    }

    for(int i = 2; i <= n; i++){
        fout << cnt[i] << " ";
    }
    fout <<endl;
}