Cod sursa(job #3264986)

Utilizator AndreiNicolaescuEric Paturan AndreiNicolaescu Data 26 decembrie 2024 11:19:50
Problema Drumuri minime Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <fstream>
#include <queue>
#include <cmath>
using namespace std;
ifstream cin("dmin.in");
ofstream cout("dmin.out");
const double INF = 1e18;
const double EPS = 1e-6;
const int MOD = 104659;
int n, m, x, y, z;
vector<vector<pair<int, double>>> graf;
vector<int> viz, ans;
vector<double> d;
void djikstra(int node)
{
    priority_queue<pair<double, int>, vector<pair<double, int>>, greater<pair<double, int>>> q;
    q.push({0.0, node});
    ans[node] = 1;
    d[node] = 0.0;
    while(!q.empty())
    {
        node = q.top().second;
        q.pop();
        if(!viz[node])
        {
            for(auto next:graf[node])
            {
                if(d[next.first] - d[node] - next.second > EPS)
                {
                     d[next.first] = d[node] + next.second;
                     ans[next.first] = ans[node];
                     q.push({d[next.first], next.first});
                }
                else if(fabs(d[node] + next.second - d[next.first] < EPS))
                    ans[next.first] = (ans[next.first] + ans[node]) % MOD;

            }


            viz[node] = 1;
        }
    }
}
int main()
{
    cin >> n >> m;
    graf.assign(n+1, vector<pair<int, double>>());
    viz.resize(n+1, 0);
    d.assign(n+1, INF);
    ans.resize(n+1, 0);
    while(m--)
    {
        cin >> x >> y >> z;
        double cost = log2(z);
        graf[x].push_back({y, cost});
        graf[y].push_back({x, cost});
    }
    djikstra(1);
    for(int i=2; i<=n; i++)
        cout << ans[i] << " ";
    return 0;
}