Cod sursa(job #2938531)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 12 noiembrie 2022 10:46:11
Problema Drumuri minime Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <fstream>
#include <queue>
#include <cmath>
#include <bitset>
#include <vector>

using namespace std;

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

struct str{
    int nod;
    double val;
    bool operator < (const str &aux) const
    {
        return val > aux.val;
    }
};

/// ideea e ca daca logaritmez, stiu ca ln(a * b) = ln a + ln b deci merge un dijkstra banal

const int max_size = 15e2 + 1, mod = 104659;
const double INF = 1e9 + 1;

long long dp[max_size], n;
double d[max_size];
vector <str> mc[max_size];
priority_queue <str> pq;
bitset <max_size> viz;

void djk ()
{
    for (int i = 1; i <= n; i++)
    {
        d[i] = INF;
    }
    d[1] = 0;
    dp[1] = 1;
    pq.push({1, 0});
    while (!pq.empty())
    {
        int nod = pq.top().nod;
        pq.pop();
        if (viz[nod])
            continue;
        for (auto f : mc[nod])
        {
            if (d[nod] + f.val < d[f.nod])
            {
                d[f.nod] = d[nod] + f.val;
                dp[f.nod] = 0;
                pq.push({f.nod, d[f.nod]});
            }
            if (d[nod] + f.val == d[f.nod])
            {
                dp[f.nod] = (dp[f.nod] + dp[nod]) % mod;
            }
        }
    }
}

int main ()
{
    int m;
    in >> n >> m;
    while (m--)
    {
        int x, y;
        double z;
        in >> x >> y >> z;
        z = log(z);
        mc[x].push_back({y, z});
        mc[y].push_back({x, z});
    }
    djk();
    for (int i = 2; i <= n; i++)
    {
        out << dp[i] << " ";
    }
    in.close();
    out.close();
    return 0;
}