Cod sursa(job #3288081)

Utilizator InfinitumDanila Laurentiu Infinitum Data 20 martie 2025 14:32:36
Problema Drumuri minime Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("dmin.in");
ofstream g("dmin.out");
//#define g cout
const double INF = 1e18, EPS = 1e-6;
const int MOD = 104659;
bitset<1501> viz;

int main()
{
    ios_base::sync_with_stdio(false);
    f.tie(nullptr);

    int n, m;
    f >> n >> m;
    vector<vector<pair<int,double>>> v(n+1);
    vector<double> d(n+1, INF);
    vector<int> cnt(n+1,0);

    priority_queue<pair<double, int>, vector<pair<double, int>>, greater<pair<double, int>>> pq;
    for(int i=1; i<=m; i++)
    {
        int x, y, c;
        f >> x >> y >> c;
        double log_c = log(c);
        v[x].push_back({y,log_c});
        v[y].push_back({x,log_c});
    }
    pq.push({0.0,1});
    d[1]=0.0;
    cnt[1]=1;
    while(!pq.empty())
    {
        double dist;
        int nod;
        tie(dist,nod) = pq.top();
        pq.pop();
        if(!viz[nod])
        {
            for(auto vc : v[nod])
            {
                int next = vc.first;
                double cost = vc.second;
                //EPS e o constanta mica care verifica ca distanta pana la nodul
                //urmator daca e mai mare sau mai mica decat cea pe care o gasim noi
                if(d[next]-(d[nod]+cost) > EPS)
                {
                    d[next]=d[nod]+cost;
                    cnt[next]=cnt[nod];
                    pq.push({d[next],next});
                }else if(abs(d[nod]+cost-d[next])<EPS)
                {
                    cnt[next]=(cnt[next]+cnt[nod])%MOD;
                }
            }
            viz[nod]=1;
        }
    }
    for(int i=2; i<=n; i++)
        g << cnt[i]%MOD << " ";

}